Posted By

nerdsane on 01/28/09


Tagged

curl php tinyurl


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

jafg
bryandease
umang_nine
rinx
jealousy
hiramvb
voove
sbcjr


Create TinyUrl


 / Published in: PHP
 

URL: http://davidwalsh.name/create-tiny-url-php

TinyURL is an awesome service. For those who don’t know what TinyURL is, TinyURL allows you to take a long URL like “http://davidwalsh.name/jquery-link-nudging” and turn it into “http://tinyurl.com/67c4se”. Using the PHP and TinyURL API, you can create these tiny URLs on the fly!

Simply provide the URL and you’ll received the new, tiny URL in return. If you use Twitter, you’ll have noticed that Twitter automates tiny URL creation for URLs in tweets.

  1. //gets the data from a URL
  2. function get_tiny_url($url)
  3. {
  4. $ch = curl_init();
  5. $timeout = 5;
  6. curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);
  7. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  8. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  9. $data = curl_exec($ch);
  10. curl_close($ch);
  11. return $data;
  12. }
  13.  
  14. //test it out!
  15. $new_url = get_tiny_url('http://davidwalsh.name/php-imdb-information-grabber');
  16.  
  17. //returns http://tinyurl.com/65gqpp
  18. echo $new_url

Report this snippet  

You need to login to post a comment.