Last active
February 13, 2024 16:39
-
-
Save eyecatchup/8495140 to your computer and use it in GitHub Desktop.
Several PHP functions to get the +1 count from Google+ users for a given URL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* GetPlusOnesByURL() | |
* | |
* Get the numeric, total count of +1s from Google+ users for a given URL. | |
* | |
* Example usage: | |
* <code> | |
* $url = 'http://www.facebook.com/'; | |
* printf("The URL '%s' received %s +1s from Google+ users.", $url, GetPlusOnesByURL($url)); | |
* </code> | |
* | |
* @author Stephan Schmitz <[email protected]> | |
* @copyright Copyright (c) 2014 Stephan Schmitz | |
* @license http://eyecatchup.mit-license.org/ MIT License | |
* @link <a href="https://gist.github.com/eyecatchup/8495140">Source</a>. | |
* @link <a href="http://stackoverflow.com/a/13385591/624466">Read more</a>. | |
* | |
* @param $url string The URL to check the +1 count for. | |
* @return intval The total count of +1s. | |
*/ | |
function GetPlusOnesByURL($url) { | |
!$url && die('No URL, no results. ;)'); | |
!filter_var($url, FILTER_VALIDATE_URL) && | |
die(sprintf('PHP said, "%s" is not a valid URL.', $url)); | |
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; | |
} | |
!$response && die("Requests to Google's server fail..?!"); | |
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Alternative version using Google's internal (NOT MEANT FOR PUBLIC USE!) RPC API. | |
/** | |
* GetPlusOnesByURLRPC() | |
* | |
* Get the numeric, total count of +1s from Google+ users for a given URL. | |
* | |
* Example usage: | |
* <code> | |
* $url = 'http://www.facebook.com/'; | |
* printf("The URL '%s' received %s +1s from Google+ users.", $url, GetPlusOnesByURLRPC($url)); | |
* </code> | |
* | |
* @author Stephan Schmitz <[email protected]> | |
* @copyright Copyright (c) 2014 Stephan Schmitz | |
* @license http://eyecatchup.mit-license.org/ MIT License | |
* @link <a href="https://gist.github.com/eyecatchup/8495140">Source</a>. | |
* @link <a href="http://stackoverflow.com/a/13385591/624466">Read more</a>. | |
* | |
* @param $url string The URL to check the +1 count for. | |
* @return intval The total count of +1s. | |
*/ | |
function GetPlusOnesByURLRPC($url) { | |
!$url && die('No URL, no results. ;)'); | |
!filter_var($url, FILTER_VALIDATE_URL) && | |
die(sprintf('PHP said, "%s" is not a valid URL.', $url)); | |
$rpcRequest = '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"%s","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]'; | |
$ch = curl_init('https://clients6.google.com/rpc'); | |
curl_setopt_array($ch, array( | |
CURLOPT_FOLLOWLOCATION => 1, | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_SSL_VERIFYPEER => 0, | |
CURLOPT_HTTPHEADER => array('Content-type: application/json'), | |
CURLOPT_POST => 1, | |
CURLOPT_POSTFIELDS => sprintf($rpcRequest, $url), | |
)); | |
$response = curl_exec($ch); | |
$curlinfo = curl_getinfo($ch); | |
curl_close($ch); | |
(200 !== $curlinfo['http_code']) && die(implode('; ', $curlinfo)); | |
$json = @json_decode($response, 1); | |
return (!$json || !isset($json[0]['result'])) ? 0 : intval($json[0]['result']['metadata']['globalCounts']['count']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment