Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active August 22, 2018 21:18
Show Gist options
  • Save igorbenic/45008e6cbb9ce03cdd69222d121171cf to your computer and use it in GitHub Desktop.
Save igorbenic/45008e6cbb9ce03cdd69222d121171cf to your computer and use it in GitHub Desktop.
How to get the Share Counts from Social Media in WordPress | www.ibenic.com/how-to-get-the-share-count-from-social-media-in-wordpress
<?php
/**
* Facebook Shares
*/
class FacebookShareCount implements Share_Counter {
public static function get_share_count( $url ) {
$facebook_app_id = "YOUR_APP_ID";
$facebook_app_secret = "YOUR_APP_SECRET";
$access_token = $facebook_app_id . '|' . $facebook_app_secret;
$check_url = 'https://graph.facebook.com/v2.7/?id=' . urlencode( $url ) . '&fields=share&access_token=' . $access_token;
$response = wp_remote_retrieve_body( wp_remote_get( $check_url ) );
$encoded_response = json_decode( $response, true );
$share_count = intval( $encoded_response['share']['share_count'] );
return $share_count;
}
}
<?php
/**
* Google+ Shares
*/
class GoogleShareCount implements Share_Counter {
public static function get_share_count( $url ) {
if( !$url ) {
return 0;
}
if ( !filter_var($url, FILTER_VALIDATE_URL) ){
return 0;
}
foreach (array('apis', 'plusone') as $host) {
$ch = curl_init(sprintf('https://%s.google.com/u/0/_/+1/fastbutton?url=%s',
$host, urlencode($url)));
curl_setopt_array($ch, array(
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64) ' .
'AppleWebKit/537.36 (KHTML, like Gecko) ' .
'Chrome/32.0.1700.72 Safari/537.36' ));
$response = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
if (200 === $curlinfo['http_code'] && 0 < strlen($response)) { break 1; }
$response = 0;
}
if( !$response ) {
return 0;
}
preg_match_all('/window\.__SSR\s\=\s\{c:\s(\d+?)\./', $response, $match, PREG_SET_ORDER);
return (1 === sizeof($match) && 2 === sizeof($match[0])) ? intval($match[0][1]) : 0;
}
}
<?php
/**
* LinkedIN Shares
*/
class LinkedINShareCount implements Share_Counter {
public static function get_share_count( $url ) {
$remote_get = json_decode( file_get_contents('https://www.linkedin.com/countserv/count/share?url=' . urlencode( $url ) . '&format=json'), true);
$share_count = $remote_get['count'];
return $share_count;
}
}
<?php
/**
* Pinterest Shares
*/
class PinterestShareCount implements Share_Counter {
public static function get_share_count( $url ) {
$check_url = 'http://api.pinterest.com/v1/urls/count.json?callback=pin&url=' . urlencode( $url );
$response = wp_remote_retrieve_body( wp_remote_get( $check_url ) );
$response = str_replace( 'pin({', '{', $response);
$response = str_replace( '})', '}', $response);
$encoded_response = json_decode( $response, true );
$share_count = intval( $encoded_response['count'] );
return $share_count;
}
}
<?php
echo FacebookShareCount::get_share_count('http://www.ibenic.com/better-wordpress-performance/') .'<br/>';
echo TwitterShareCount::get_share_count('http://www.ibenic.com/better-wordpress-performance/') .'<br/>';
echo LinkedINShareCount::get_share_count('http://www.ibenic.com/better-wordpress-performance/') .'<br/>';
echo GoogleShareCount::get_share_count('http://www.ibenic.com/better-wordpress-performance/') .'<br/>';
echo PinterestShareCount::get_share_count('http://www.ibenic.com/better-wordpress-performance/') .'<br/>';
echo StumbleUponShareCount::get_share_count('http://www.ibenic.com/better-wordpress-performance/') .'<br/>';
/**
* Result:
* 9
* 1
* 4
* 9
* 52
*/
<?php
/**
* Abstract class for counting shares
*/
interface Share_Counter {
/**
* Getting the share count
*/
public static function get_share_count( $url );
}
<?php
/**
* StumbleUpon Shares
*/
class StumbleUponShareCount implements Share_Counter {
public static function get_share_count( $url ) {
$check_url = 'http://www.stumbleupon.com/services/1.01/badge.getinfo?url=' . urlencode( $url );
$response = wp_remote_retrieve_body( wp_remote_get( $check_url ) );
$encoded_response = json_decode( $response, true );
$share_count = intval( $encoded_response['result']['views'] );
return $share_count;
}
}
<?php
/**
* Twitter Shares
*/
class TwitterShareCount implements Share_Counter {
public static function get_share_count( $url ) {
$check_url = 'http://public.newsharecounts.com/count.json?url=' . urlencode( $url );
$response = wp_remote_retrieve_body( wp_remote_get( $check_url ) );
$encoded_response = json_decode( $response, true );
$share_count = intval( $encoded_response['count'] );
return $share_count;
}
}
@DonPramis
Copy link

Ii follow your code..but i cant make it work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment