Wednesday, 1 October 2014

How to get the social sharing counter for facebook, twitter, gplus and pinterest


How to get the social sharing counter for facebook, twitter, gplus and pinterest

Below I will show you how you can get the social count numbers from each of the social share.Here I have used API request for all social sharing counter and used file_get_contents function for fetch data....

Facebook :

$query = "SELECT url, normalized_url, share_count,total_count, commentsbox_count, comments_fbid, click_count FROM link_stat WHERE url = '".$_productUrl."'";
$apiquery = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode($query);
$json = file_get_contents($apiquery);
$cnt = json_decode($json);
echo intval($cnt[0]->share_count);

Twitter :

$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $_productUrl);
$json = json_decode($json_string, true);
echo intval($json['count']);


Google plus :

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.rawurldecode($_productUrl).'","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);
echo isset($json[0]['result']['metadata']['globalCounts']['count'])?intval( $json[0]['result']['metadata']['globalCounts']['count'] ):0;

Pinterest

$pinfo = json_decode(preg_replace('/^receiveCount\((.*)\)$/', "\\1",file_get_contents('http://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url='.$_productUrl)));
echo isset($pinfo->count) ? $pinfo->count : 0;


Thats it enjoy chandresh rana coding... :)

No comments: