Skip to content

Instantly share code, notes, and snippets.

@KWMalik
Forked from mhawksey/gist:3833072
Created October 12, 2012 21:38
Show Gist options
  • Save KWMalik/3881669 to your computer and use it in GitHub Desktop.
Save KWMalik/3881669 to your computer and use it in GitHub Desktop.
Bulk collect social share counts from Facebook, Twitter, Delicious, Pinterest and Google +1s and return json
<?php
$url = $_GET['url'];
$finfo = json_decode(file_get_contents('http://api.ak.facebook.com/restserver.php?v=1.0&method=links.getStats&urls='.$url.'&format=json'));
$tinfo = json_decode(file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url='.$url));
$dinfo = json_decode(file_get_contents('http://feeds.delicious.com/v2/json/urlinfo/data?url='.$url));
$pinfo = json_decode(preg_replace('/^receiveCount\((.*)\)$/', "\\1",file_get_contents('http://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url='.$url)));
$gplus = gplus_shares($url);
//http://papermashup.com/google-plus-php-function/
function gplus_shares($url){
// G+ DATA
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, 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($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$result = curl_exec ($ch);
curl_close ($ch);
return json_decode($result, true);
}
$output = array(
'facebook'=> isset($finfo[0]) ? $finfo[0]->total_count : NULL,
'twitter'=> isset($tinfo->count) ? $tinfo->count : NULL,
'delicious'=> isset($dinfo[0]) ? $dinfo[0]->total_posts : NULL,
'pinterest'=> isset($pinfo->count) ? $pinfo->count : NULL,
'googlePlus'=> isset($gplus[0]['result']) ? $gplus[0]['result']['metadata']['globalCounts']['count'] : NULL
);
header('Content-Type: text/javascript');
echo "[".json_encode($output)."]";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment