Thursday 19 September 2013

How To Add Tweeting With Short URL Functionality To A Website (With PHP)


There is a 140 character limit in Twitter & the length of the URL is a headache. So, auto-shortening the URL with a short URL service will be handy.

The file_get_contents function of PHP helps us to easily get the short URL as a string. Lets use the TinyURLservice & create a simple function:
<?php
function makeShortURL($URLToConvert) { 
     $shortURL= file_get_contents("http://tinyurl.com/api-create.php?url=" . $URLToConvert); 
     return $shortURL; 
}
?>
calling the function with the URL, like:
<?php echo makeShortURL("http://knowledgecornor.blogspot.in/2013/08/how-to-send-sms-using-php_15.html")?>

Result: http://tinyurl.com/lh8znkd