|
<?php |
|
|
|
function getSocialSharesFor($url){ |
|
$results = []; |
|
|
|
// Facebook |
|
$data = json_decode(file_get_contents('https://graph.facebook.com/'.urlencode($url))); |
|
$results['facebook'] = isset($data->shares)?intval($data->shares):0; |
|
|
|
// Twitter |
|
$data = json_decode(file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url='.urlencode($url))); |
|
$results['twitter'] = isset($data->count)?intval($data->count):0; |
|
|
|
// Google+ |
|
// https://gist.github.com/eyecatchup/8495140 |
|
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){ |
|
preg_match_all('/window\.__SSR\s\=\s\{c:\s(\d+?)\./', $response, $match, PREG_SET_ORDER); |
|
$results['googleplus'] = (1 === sizeof($match) && 2 === sizeof($match[0])) ? intval($match[0][1]) : 0; |
|
} else $results['googleplus'] = 0; |
|
|
|
|
|
// Pinterest |
|
$data = json_decode(preg_replace('/^receiveCount\((.*)\)$/', '$1', |
|
file_get_contents('http://api.pinterest.com/v1/urls/count.json?url='.$url))); |
|
$results['pinterest'] = isset($data->count)?intval($data->count):0; |
|
|
|
// Linkedin |
|
$data = json_decode(file_get_contents('http://www.linkedin.com/countserv/count/share?url='.urlencode($url).'&format=json')); |
|
$results['linkedin'] = isset($data->count)?intval($data->count):0; |
|
|
|
|
|
$results['total'] = array_sum($results); |
|
return $results; |
|
} |
|
|
|
|
|
|
|
|
|
print_r( |
|
getSocialSharesFor('http://www.apple.com') |
|
); |
|
|
|
/* |
|
Array |
|
( |
|
[facebook] => 396727 |
|
[twitter] => 1164819 |
|
[googleplus] => 28323 |
|
[pinterest] => 715 |
|
[linkedin] => 703 |
|
[total] => 1591287 |
|
) |
|
*/ |