Twitter share button and Facebook share button are the most used buttons to share links on Internet. You can read Facebook count as I’ve explained here, and you can also read the number of the times that your urls have been tweeted on Twitter.
The code is very simple because there is a very simple public api to call. So, let’s call the twitter api, and parse the results. The results are parsed with the preg_match function but you can use also the json_decode function, probably it is better. As you can see there is no need to use curl library, we can just use file_get_contents.
If you store this count and the facebook total like count, you can show those data better on your site and you can use those data to make some “top stories” list.
function readTwitterShares($url) { $s = file_get_contents("http://urls.api.twitter.com/1/urls/count.json". "?callback=?&url=".urlencode($url)); preg_match("#(\"count\"):([0-9]*)#",$s,$ar); return isset($ar[2]) ? $ar[2] : 0; } echo readTwitterShares("http://www.dailybest.it");
This function will be soon included in the Mini Bots Class.