我想在PHP中获取google plus上的给定URL的份额数。我找到了这个功能:
function get_shares_google_plus($url) {
$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":"' . $url . '","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);
print_r($json);
return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
}
但是,我总是收到同样的消息:Notice: Undefined index: result in ...
。
我制作print_r($json)
,我得到:Array ( [0] => Array ( [error] => Array ( [code] => 400 [message] => Invalid Value [data] => Array ( [0] => Array ( [domain] => global [reason] => invalid [message] => Invalid Value ) ) ) [id] => p )
。
有什么建议吗?
答案 0 :(得分:8)
RPC API从未打算供公众使用,Google更改了身份验证以防止滥用。因此,您发布的代码不再起作用。但是,我找到了一个更简单的解决方案:
更新(2013年1月23日): Google于2012年12月封锁了此网址 - 因此此方法不再有效!
更新(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;
}
更新(2014年1月18日):这是一个改进版本,它使用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在this Google+ Post中宣布的那样,Google最近从其网络共享按钮中删除了共享计数(又名+1 Count)。 (他们声称此举的目的是让+1按钮和共享框加载更快。)
答案 1 :(得分:1)
此代码不起作用。此外,还没有提供此计数的公开API。
此代码使用为+1按钮提供动力的RPC API。此API不是官方支持的API,不应在Google+ plugins的内部实现之外使用。
答案 2 :(得分:0)
此处其他帖子中列出的cURL和API方式不再有效。
There is still at least 1 method,但它很难看,Google显然不支持它。您只需使用正则表达式从官方按钮的JavaScript源代码中删除变量:
function shinra_gplus_get_count( $url ) {
$contents = file_get_contents(
'https://plusone.google.com/_/+1/fastbutton?url='
. urlencode( $url )
);
preg_match( '/window\.__SSR = {c: ([\d]+)/', $contents, $matches );
if( isset( $matches[0] ) )
return (int) str_replace( 'window.__SSR = {c: ', '', $matches[0] );
return 0;
}