|
<?php |
|
|
|
class SocialCount |
|
{ |
|
|
|
public static function getAll($link) |
|
{ |
|
return self::getServices($link); |
|
} |
|
|
|
public static function getServices($link, $services='facebook,twitter,gplus') |
|
{ |
|
$services = explode(',', $services); |
|
$r = (object)array(); |
|
foreach ($services as $service) |
|
{ |
|
if ($service=='facebook') $r->facebook = self::getFacebook($link); |
|
elseif ($service=='twitter') $r->twitter = self::getTwitter($link); |
|
elseif ($service=='gplus') $r->gplus = self::getGplus($link); |
|
} |
|
return $r; |
|
} |
|
|
|
public static function getFacebook($link) |
|
{ |
|
$link = urlencode($link); |
|
$data = @file_get_contents("http://graph.facebook.com/?id=$link"); |
|
|
|
$json = @json_decode($data, true); |
|
if (!$json) return 0; |
|
|
|
$count = $json["shares"]; |
|
return $count ? $count : 0; |
|
} |
|
|
|
public static function getTwitter($link) |
|
{ |
|
$link = urlencode($link); |
|
$data = @file_get_contents("http://urls.api.twitter.com/1/urls/count.json?url=$link"); |
|
|
|
$json = @json_decode($data, true); |
|
if (!$json) return 0; |
|
|
|
$count = $json["count"]; |
|
return $count ? $count : 0; |
|
} |
|
|
|
public static function getGplus($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); |
|
if (!$json) return 0; |
|
|
|
$count = $json[0]['result']['metadata']['globalCounts']['count']; |
|
return $count ? $count : 0; |
|
} |
|
|
|
public static function getYoutube($user) |
|
{ |
|
$link = urlencode($link); |
|
$data = @file_get_contents("http://gdata.youtube.com/feeds/api/users/$user?alt=json"); |
|
|
|
$json = @json_decode($data, true); |
|
if (!$json) return 0; |
|
|
|
$count = $json['entry']['yt$statistics']['subscriberCount']; |
|
return $count ? $count : 0; |
|
} |
|
|
|
}; |
|
|
|
|
|
/* |
|
Simple Usage: |
|
|
|
$count = SocialCount::getServices('http://google.it', 'twitter,facebook'); |
|
echo "Twitter: {$count->twitter}, "; |
|
echo "Facebook: {$count->facebook}"; |
|
|
|
*/ |
Where to edit to use my facebook/twitter/googleplus ID or username?
sorry i am newbie.