Friday, June 15, 2012

Shorten a URL using Bit.ly api

0
Shorten a URL using Bit.ly.
Bit.ly is an URL shortener service, and it allow us to shorten url using their API. And we can connect to their API using PHP Curl:
 
$longurl='http://webizone.blogspot.com'; 
$login='your bit.ly username';
$appkey='your app key';
$api_url = 'http://api.bit.ly/shorten?version=2.0.1&
longUrl='.urlencode($longurl).'&
format=xml&login='.$login.'&apiKey='.$appkey; //call the API
 $curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 $response = curl_ex_ec($curl); curl_close($curl);
 //parse the XML response and return the url
$xml_object = new SimpleXMLElement($response);
echo $xml_object->results->nodeKeyVal->shortUrl; //output the shortened url

No comments :

Post a Comment