RPC API 从未打算供公众使用,Google 更改了身份验证以防止滥用。因此,您发布的代码不再起作用。但是,我找到了一个更简单的解决方案:
更新 (23.01.2013):谷歌在 2012 年 12 月阻止了这个 URL - 所以这个方法不再起作用了!
更新(15.05.2013):该方法再次有效!
<?php
/**
* Get the numeric, total count of +1s from Google+ users for a given URL.
* @author Stephan Schmitz <eyecatchup@gmail.com>
* @copyright Copyright (c) 2013 Stephan Schmitz
* @license http://eyecatchup.mit-license.org/ MIT License
* @link <a href="https://gist.github.com/eyecatchup/8495140">Source</a>.
* @param $url string The URL to check the +1 count for.
* @return intval The total count of +1s.
*/
function getGplusShares($url) {
$url = sprintf('https://plusone.google.com/u/0/_/+1/fastbutton?url=%s', urlencode($url));
preg_match_all('/{c: (.*?),/', file_get_contents($url), $match, PREG_SET_ORDER);
return (1 === sizeof($match) && 2 === sizeof($match[0])) ? intval($match[0][1]) : 0;
}
更新 (18.01.2014):这是一个使用 curl 的改进版本,一个后备主机并进行一些错误处理(最新版本可以在这里找到https://gist.github.com/eyecatchup/8495140)。
<?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 <eyecatchup@gmail.com>
* @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;
}
更新 (02.11.2017): +1 计数正式失效!正如产品经理 John Nack 在此 Google+ 帖子中所宣布的那样,Google 最近从其网络共享按钮中删除了共享计数(又名 +1 计数)。(他们声称此举的目的是让 +1 按钮和分享框加载更快。)