Skip to content

Instantly share code, notes, and snippets.

@jasondavis
Last active September 26, 2021 22:05
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jasondavis/72a65a49a041dc8a692a to your computer and use it in GitHub Desktop.
Save jasondavis/72a65a49a041dc8a692a to your computer and use it in GitHub Desktop.
Social Network Share Count for a URL - PHP Class
<?php
/*
Example Usage passing in Array of netowrks we want countsa for...
$socialCounts = new socialNetworkShareCount(array(
'url' => 'http://www.codedevelopr.com/',
'facebook' => true,
'twitter' => true,
'pinterest' => true,
'linkedin' => true,
'google' => true
));
print_r($socialCounts->getShareCounts());
Result:
Array
(
[facebookshares] => 8
[facebooklikes] => 0
[twittershares] => 58
[pinterestshares] => 52
[linkedinshares] => 17
[googleplusones] => 0
[total] => 135
)
Another website that returns the same data that this class gets on our own... http://www.sharedcount.com
*/
// Demo Usage....
// Setting which networks to get share counts from using the network name in the array passed in...
$socialCounts1 = new socialNetworkShareCount(array(
'url' => 'http://www.codedevelopr.com/',
'facebook' => true,
'twitter' => true,
'pinterest' => true,
'linkedin' => true,
'google' => true
));
echo '<pre>';
print_r($socialCounts1->getShareCounts());
echo '<hr>';
// Passing URL as a string and no array which returns ALL SOcial Netoworks!
$socialCounts2 = new socialNetworkShareCount('http://www.codedevelopr.com/');
echo '<pre>';
print_r($socialCounts2->getShareCounts());
echo '<hr>';
// Only fetching and returning some of the network share counts......
$socialCounts3 = new socialNetworkShareCount(array(
'url' => 'http://www.codedevelopr.com/',
'facebook' => true,
'twitter' => true
));
echo '<pre>';
print_r($socialCounts3->getShareCounts());
echo '<hr>';
<?php
/**
* Get Social Share Counts for a URL from the majopr Social Networks
* @var Mixed String or Array $options['url'] = URL that we want to get the social share counts for
* @var $options['facebook] = if Array Key set, then return share count from this social network
* @var $options['twitter'] = if Array Key set, then return share count from this social network
* @var $options['pinterest'] = if Array Key set, then return share count from this social network
* @var $options['linkedin'] = if Array Key set, then return share count from this social network
* @var $options['google'] = if Array Key set, then return share count from this social network
*
* If no Social Network Array keys are passed then it will return the counts from all the networks.
*/
class socialNetworkShareCount{
public $shareUrl;
public $socialCounts = array();
public $facebookShareCount = 0;
public $facebookLikeCount = 0;
public $twitterShareCount = 0;
public $pinterestShareCount = 0;
public $linkedInShareCount = 0;
public $googlePlusOnesCount = 0;
public function __construct($options){
if(is_array($options)){
if(array_key_exists('url', $options) && $options['url'] != ''){
$this->shareUrl = $options['url'];
}else{
die('URL must be set in constructor parameter array!');
}
// Get Facebook Shares and Likes
if(array_key_exists('facebook', $options)){
$this->getFacebookShares();
$this->getFacebookLikes();
}
// Get Twitter Shares
if(array_key_exists('twitter', $options)){
$this->getTwitterShares();
}
// Get Twitter Shares
if(array_key_exists('pinterest', $options)){
$this->getPinterestShares();
}
// Get Twitter Shares
if(array_key_exists('linkedin', $options)){
$this->getLinkedInShares();
}
// Get Twitter Shares
if(array_key_exists('google', $options)){
$this->getGooglePlusOnes();
}
}elseif(is_string($options) && $options != ''){
$this->shareUrl = $options;
// Get all Social Network share counts if they are not set individually in the options
$this->getFacebookShares();
$this->getFacebookLikes();
$this->getTwitterShares();
$this->getPinterestShares();
$this->getLinkedInShares();
$this->getGooglePlusOnes();
}else{
die('URL must be set in constructor parameter!');
}
}
public function getShareCounts(){
$totalShares = $this->getTotalShareCount($this->socialCounts);
$this->socialCounts['total'] = $totalShares;
return $this->socialCounts;
}
public function getTotalShareCount(array $shareCountsArray){
return array_sum($shareCountsArray);
}
public function getFacebookShares(){
$api = file_get_contents( 'http://graph.facebook.com/?id=' . $this->shareUrl );
$count = json_decode( $api );
if(isset($count->shares) && $count->shares != '0'){
$this->facebookShareCount = $count->shares;
}
$this->socialCounts['facebookshares'] = $this->facebookShareCount;
return $this->facebookShareCount;
}
public function getFacebookLikes(){
$api = file_get_contents( 'http://graph.facebook.com/?id=' . $this->shareUrl );
$count = json_decode( $api );
if(isset($count->likes) && $count->likes != '0'){
$this->facebookLikeCount = $count->likes;
}
$this->socialCounts['facebooklikes'] = $this->facebookLikeCount;
return $this->facebookLikeCount;
}
public function getTwitterShares(){
$api = file_get_contents( 'https://cdn.api.twitter.com/1/urls/count.json?url=' . $this->shareUrl );
$count = json_decode( $api );
if(isset($count->count) && $count->count != '0'){
$this->twitterShareCount = $count->count;
}
$this->socialCounts['twittershares'] = $this->twitterShareCount;
return $this->twitterShareCount;
}
public function getPinterestShares(){
$api = file_get_contents( 'http://api.pinterest.com/v1/urls/count.json?callback%20&url=' . $this->shareUrl );
$body = preg_replace( '/^receiveCount\((.*)\)$/', '\\1', $api );
$count = json_decode( $body );
if(isset($count->count) && $count->count != '0'){
$this->pinterestShareCount = $count->count;
}
$this->socialCounts['pinterestshares'] = $this->pinterestShareCount;
return $this->pinterestShareCount;
}
public function getLinkedInShares(){
$api = file_get_contents( 'https://www.linkedin.com/countserv/count/share?url=' . $this->shareUrl . '&format=json' );
$count = json_decode( $api );
if(isset($count->count) && $count->count != '0'){
$this->linkedInShareCount = $count->count;
}
$this->socialCounts['linkedinshares'] = $this->linkedInShareCount;
return $this->linkedInShareCount;
}
public function getGooglePlusOnes(){
if(function_exists('curl_version')){
$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":"' . $this->shareUrl . '","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 );
$this->googlePlusOnesCount = intval( $json[0]['result']['metadata']['globalCounts']['count'] );
}else{
$content = file_get_contents("https://plusone.google.com/u/0/_/+1/fastbutton?url=".urlencode($_GET['url'])."&count=true");
$doc = new DOMdocument();
libxml_use_internal_errors(true);
$doc->loadHTML($content);
$doc->saveHTML();
$num = $doc->getElementById('aggregateCount')->textContent;
if($num){
$this->googlePlusOnesCount = intval($num);
}
}
$this->socialCounts['googleplusones'] = $this->googlePlusOnesCount;
return $this->googlePlusOnesCount;
}
}
@Kamleshpaul
Copy link

it's not working

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