Skip to content

Instantly share code, notes, and snippets.

@askfriends
Forked from kopiro/get-social-counts.php
Created November 12, 2012 06:24
Show Gist options
  • Save askfriends/4057811 to your computer and use it in GitHub Desktop.
Save askfriends/4057811 to your computer and use it in GitHub Desktop.
Get social counts from Facebook, Twitter, GPlus
<?php
/* Return object of shared counts */
function get_social_count( $link ) {
$r = (object)array();
$r->facebook = get_social_count_facebook($link);
$r->twitter = get_social_count_twitter($link);
$r->gplus = get_social_count_gplus($link);
return $r;
}
/* Return shared counts */
function get_social_count_facebook( $link ) {
$link = urlencode($link);
$data = file_get_contents("http://graph.facebook.com/?id=$link");
$json = json_decode($data, true);
$count = $json["shares"];
return $count ? $count : 0;
}
/* Return retweet counts */
function get_social_count_twitter( $link ) {
$link = urlencode($link);
$data = file_get_contents("http://urls.api.twitter.com/1/urls/count.json?url={$link}");
$json = json_decode($data, true);
$count = $json["count"];
return $count ? $count : 0;
}
/* Return shared counts */
function get_social_count_gplus( $link ) {
$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_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.$link.'","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'));
$data = curl_exec ($ch);
curl_close ($ch);
$json = json_decode($data, true);
$count = $json[0]['result']['metadata']['globalCounts']['count'];
return $count ? $count : 0;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment