Skip to content

Instantly share code, notes, and snippets.

@whyisjake
Created January 5, 2012 20:45
Show Gist options
  • Save whyisjake/1567203 to your computer and use it in GitHub Desktop.
Save whyisjake/1567203 to your computer and use it in GitHub Desktop.
Tweet, Like, and Plus Tracker
error_reporting(E_ALL);
$url = 'http://makeprojects.com/Project/Kitty-Twitty-Cat-Toy/1439/1';
function get_tweets($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'] );
}
function get_likes($url) {
$json_string = file_get_contents('http://graph.facebook.com/?ids=' . $url);
$json = json_decode($json_string, true);
return intval( $json[$url]['shares'] );
}
function get_plusones($url) {
$args = array(
'method' => 'POST',
'headers' => array(
// setup content type to JSON
'Content-Type' => 'application/json'
),
// setup POST options to Google API
'body' => json_encode(array(
'method' => 'pos.plusones.get',
'id' => 'p',
'method' => 'pos.plusones.get',
'jsonrpc' => '2.0',
'key' => 'p',
'apiVersion' => 'v1',
'params' => array(
'nolog'=>true,
'id'=> $url,
'source'=>'widget',
'userId'=>'@viewer',
'groupId'=>'@self'
)
)),
// disable checking SSL sertificates
'sslverify'=>false
);
// retrieves JSON with HTTP POST method for current URL
$json_string = wp_remote_post("https://clients6.google.com/rpc", $args);
if (is_wp_error($json_string)){
// return zero if response is error
return "0";
} else {
$json = json_decode($json_string['body'], true);
// return count of Google +1 for requsted URL
return intval( $json['result']['metadata']['globalCounts']['count'] );
}
}
echo get_tweets('http://makeprojects.com/Project/Kitty-Twitty-Cat-Toy/1439/1');
echo '<br />';
echo get_likes('http://makeprojects.com/Project/Kitty-Twitty-Cat-Toy/1439/1');
echo '<br />';
echo get_plusones('http://makeprojects.com/Project/Kitty-Twitty-Cat-Toy/1439/1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment