Creating a social share Counter in PHP

Few days back, I was working on PHP script to count the number of times a URL was shared on different social network. So, I thought to share it with you all. In this script, you only need to enter the URL and it will show you the share count in Twitter, Facebook, Linkedin, Google+, Stumbleupon, Pinterest, delicious and buffer. Of course you can add more if you want.
But I tried basically only for these social websites. Approach was pretty simple. All websites offers their APIs to count these things. I only used those things.
If you have ever worked with APIs, you can easily understand how the things are working. For example, I created this function for Twitter.
function get_tweets() { $json_string = $this->file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url); $json = json_decode($json_string, true); return isset($json['count'])?intval($json['count']):0; }
The above function will return the count of tweets a URL got on Twitter.
A similar kind of code for linkedin
function get_linkedin() { $json_string = $this->file_get_contents_curl("http://www.linkedin.com/countserv/count/share?url=$this->url&format=json"); $json = json_decode($json_string, true); return isset($json['count'])?intval($json['count']):0; }
The above function will fetch the LinkedIn share cont.
function get_fb() { $json_string = $this->file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$this->url); $json = json_decode($json_string, true); return isset($json[0]['total_count'])?intval($json[0]['total_count']):0; }
This function is for counting Facebook share.
You can download the full working code here
Please share this tutorial with your friends and help us.
Leave a comment
Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.