How to get Share Count from Facebook, Twitter and Google+

social media facebook twitter google+Getting the social media share count for any URL can be useful for many thing in a website, you can use that for statistical purpose or for display.  The social media share count is basically the number of times a webpage was shared on a certain social media.Getting the number of share for each of the main 3 social media is done using JSON calls to the API each of those social media provide. First of all remember you must call before everything the jquery library, if you haven’t done so yet here is a sample code

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js”></script>

facebookGetting the number of share on Facebook

This function will return a string with the number of time the URL was shared.

function GetFacebookShares($url) {
$json_string = file_get_contents(‘http://graph.facebook.com/?ids=’ . $url);
$json = json_decode($json_string, true);
return intval( $json[$url][‘shares’] );
}

twitter_32Getting the number of tweets from Twitter

This function will return a string with the number tweets for a certain URL.

function GetTwitterTweets($url) {
$json_string = file_get_contents(‘http://urls.api.twitter.com/1/urls/count.json?url=’ . $url);
$json = json_decode($json_string, true);
return intval( $json[‘count’] );
}

googleGetting the number of time the URL was shared on Google+

This function will return a string with the number of times the URL was shared

function GetGooglePlusShares($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, “https://clients6.google.com/rpc”);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, ‘[{“method”:”pos.plusones.get”,”id”:”p”,”params”:{“nolog”:true,”id”:”‘ . $url . ‘”,”source”:”widget”,”userId”:”@viewer”,”groupId”:”@self”},”jsonrpc”:”2.0″,”key”:”p”,”apiVersion”:”v1″}]’);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(‘Content-type: application/json’));
$curl_results = curl_exec ($curl);
curl_close ($curl);
$json = json_decode($curl_results, true);
return intval( $json[0][‘result’][‘metadata’][‘globalCounts’][‘count’] );
}

Hope the above code can help you solve your problem, if you have any comment please don’t hesitate, you can use the comments below or the contact us page.

Leave a Reply