How many times a web link has been shared on Twitter

Twitter share button and Facebook share button are the most used buttons to share links on Internet. You can read…

October 19, 2012

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.

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “How many times a web link has been shared on Twitter”

Recommended

get MySpace events with a PHP function

Here is a function to read the concerts for a myspace band page. This code retrieves the “shows page” for…

February 21, 2011

New version of Mini Bots PHP Class (v.1.4)

I’ve added three more bots to the Mini Bots Php Class, now the version number is 1.4 and it has…

January 20, 2010

How to read facebook likes count from PHP

When you add facebook like button to your site, probably, you also want to save the number of likes of…

October 8, 2012

PHP to get twitter infos and avatar

I’ve just updated the Mini Bot Php Class with an improved version of the twitterInfo function, here is the code…

March 1, 2010

PHP Web page to text function

I’ve found this nice small bot on the www.php.net site, thanks to the author of the script on the preg_replace…

January 16, 2010

Bot that retrieves url meta data and other infos

From a given url this function retrieves page title, meta description, keywords, favicon, and an array of 5 images to…

January 12, 2010