Skip to content

Instantly share code, notes, and snippets.

@mrkpatchaa
Created October 6, 2014 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrkpatchaa/553c324ec107eb36d90e to your computer and use it in GitHub Desktop.
Save mrkpatchaa/553c324ec107eb36d90e to your computer and use it in GitHub Desktop.
Getting Social Network Share Count from Facebook, Twitter, Pinterest, Linkedin, Google Plus, StumbleUpon
// Facebook Likes and Shares
$facebook_like_share_count = function ( $url ) {
$api = file_get_contents( 'http://graph.facebook.com/?id=' . $url );
$count = json_decode( $api );
return $count->shares;
};
// Pages
$facebook_page_like_count = function ( $url ) {
$api = file_get_contents( 'http://graph.facebook.com/?id=' . $url );
$count = json_decode( $api );
return $count->likes;
};
echo $facebook_page_like_count( 'http://www.facebook.com/designmodo' );
// Twitter
$twitter_tweet_count = function ( $url ) {
$api = file_get_contents( 'https://cdn.api.twitter.com/1/urls/count.json?url=' . $url );
$count = json_decode( $api );
return $count->count;
};
echo $twitter_tweet_count( 'http://designmodo.com/wordpress-https/' );
// Pinterest
$pinterest_pins = function ( $url ) {
$api = file_get_contents( 'http://api.pinterest.com/v1/urls/count.json?callback%20&url=' . $url );
$body = preg_replace( '/^receiveCount\((.*)\)$/', '\\1', $api );
$count = json_decode( $body );
return $count->count;
};
echo $pinterest_pins( 'http://designmodo.com/' );
// Linkedin
$linkedin_share = function ( $url ) {
$api = file_get_contents( 'https://www.linkedin.com/countserv/count/share?url=' . $url . '&format=json' );
$count = json_decode( $api );
return $count->count;
};
echo $linkedin_share( 'http://designmodo.com/' );
// StumbleUpon
$stumbleupon = function ( $url ) {
$api = file_get_contents( 'https://www.stumbleupon.com/services/1.01/badge.getinfo?url=' . $url );
$count = json_decode( $api );
return $count->result->views;
};
echo $stumbleupon ( 'http://designmodo.com/' );
// Google Plus
$google_plusones = function ( $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'] );
};
echo $google_plusones('http://designmodo.com/wordpress-https');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment