Convert long url to short url using tinyurl api

Tinyurl provides us a short and simple service to convert long URL to small(tiny) URL. We can send email without link breakage.
Here is the tinyurl api to get shorten url. You can use any of the method.

Method 1:

Use file_get_contents to fetch tinyurl.


echo file_get_contents('http://tinyurl.com/api-create.php?url=http://www.websnippetz.com');

file_get_contents is not working? If your are able to access php.ini file set allow_url_fopen = On or you can use the below method.

Method 2:

Use cURL(runs faster than file_get_contents) to fetch tinyurl.


function get_tiny_url($url) {
$url = "http://tinyurl.com/api-create.php?url={$url}";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_tiny_url('http://www.websnippetz.com');

2 Comments

 Add your comment
  1. […] and a tip of the hat to WEBSNIPPETZ : Convert long url to short url using tinyurl api Previous Topic: Tempe Daytime Photo […]

  2. […] and a tip of the hat to WEBSNIPPETZ : Convert long url to short url using tinyurl api Previous Topic: Tempe Daytime Photo […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: