Do you know a bit.ly API Php Script example?

DomingoSL picture DomingoSL · May 19, 2010 · Viewed 9.1k times · Source

Do you have a php script that use the API from bit.ly ?

Answer

Soufiane Hassou picture Soufiane Hassou · May 19, 2010

I just googled your question : Example code

/* Example code */  
$link = "http://www.stackoverflow.com";  

print getSmallLink($link);  

function getSmallLink($longurl){  
// Bit.ly  
$url = "http://api.bit.ly/shorten?version=2.0.1&longUrl=$longurl&login=YOURLOGIN&apiKey=YOURAPIKEY&format=json&history=1";  

$s = curl_init();  
curl_setopt($s,CURLOPT_URL, $url);  
curl_setopt($s,CURLOPT_HEADER,false);  
curl_setopt($s,CURLOPT_RETURNTRANSFER,1);  
$result = curl_exec($s);  
curl_close( $s );  

$obj = json_decode($result, true);  
return $obj["results"]["$longurl"]["shortUrl"];  
}