Get cool snippets on php, css, jQuery, Magento and wordpress

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;
$data = curl_exec($ch);
return $data;
echo get_tiny_url('http://www.websnippetz.com');

karthi s

2 thoughts on “Convert long url to short url using tinyurl api

Leave a Reply

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

%d bloggers like this: