How To: Display Number of del.icio.us (delicious) Saves in PHP

I was trying to hax up a site, and wanted to get Delicious to show the number of links already in their db.  So after a bit of reading from Tagometer Widget and some useful code (which didn’t work), I came up with this PHP code, that works.

function getDeliciousCount($permalink=''){
 if($permalink!=''){
 $hash = md5($permalink);
 }
 else {

 $hash = md5("http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
 }

 $url = "http://feeds.delicious.com/v2/json/urlinfo?callback=displayTotalSaves&hash=".$hash;
 $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);
 preg_match("/\\\"total_posts\"\:([0-9]+)/",$data,$saves);

 if (!$saves[1])
 return "0";
 else
 return $saves[1];
}

~ by Amrix on July 12, 2009.

Leave a Reply