问题
I've created an POST request to pull the share count for a specific URL from Google Plus but have been unable to get any count value returned.
Am I going the long way about it or is there an easier way to retrieve this data, for example Facebook and Twitter supply a URL that you pass the URL to that you want to retrieve the share count for and it returns a result in a JSON format!?
GOOGLE_API_KEY
id a defined variable of the key I've gotten from Google's developer console for Google+ API
.
$vRequest = '[{"method":"pos.plusones.get",
"id":"p",
"params":{
"nolog":true,
"id":"http://www.google.com",
"source":"widget",
"userId":"@viewer",
"groupId":"@self"
},
"jsonrpc":"2.0",
"key":"p",
"apiVersion":"v1"}]';
$vUrl = "https://clients6.google.com/rpc?key=".GOOGLE_API_KEY;
$vOptions = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => $vRequest,
),
);
$vContext = stream_context_create($vOptions);
$vResponse = json_decode(@file_get_contents($vUrl, false, $vContext));
The JSON response I get back is:
array(1) {
[0]=> object(stdClass)#1 (2) {
["error"]=> object(stdClass)#2 (3) {
["code"]=> int(403)
["message"]=> string(143) "Access Not Configured. The API (+1 API) is not enabled for your project. Please use the Google Developers Console to update your configuration."
["data"]=> array(1) {
[0]=> object(stdClass)#3 (4) {
["domain"]=> string(11) "usageLimits"
["reason"]=> string(19) "accessNotConfigured"
["message"]=> string(143) "Access Not Configured. The API (+1 API) is not enabled for your project. Please use the Google Developers Console to update your configuration."
["extendedHelp"]=> string(37) "https://console.developers.google.com"
}
}
}
["id"]=> string(1) "p"
}
}
Now I've checked Google's API console and both Google+ API
and Google+ Domains API
are both enabled, not sure what else I'm missing.
回答1:
That is a private API that Google has not made available to the general public. You application is not whitelisted to use it. There is an open feature request for getting the +1 count of a URL.
回答2:
Turns out the code I was using above doesn't work any more, Google made a change in 2013 to block it being used, the code I'm using now to get the approximate share count is below.
$vHtml = @file_get_contents( "https://plusone.google.com/_/+1/fastbutton?url=".urlencode("http://www.google.com"));
$vDoc = new DOMDocument();
@$vDoc->loadHTML($vHtml);
$vCounter = $vDoc->getElementById('aggregateCount');
echo $vCounter->nodeValue;
At the time of posting this there is no way to get the share count from Google Plus as Google haven't made it available via any API yet.
来源:https://stackoverflow.com/questions/28203030/google-plus-api-count-call-failing