难以获得谷歌加一计数

时间:2011-09-06 14:03:41

标签: php google-plus-one

我一直在网上搜索如何获得programaticaly谷歌加一键计数。 最后我找到了article 这是arcticle中提到的Php脚本。

<?php

 $url = "http://www.tomanthony.co.uk/";

 $ch = curl_init();   
 curl_setopt($ch, CURLOPT_URL, "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ"); 
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, 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($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));


 $curl_results = curl_exec ($ch);
 curl_close ($ch);

 $parsed_results = json_decode($curl_results, true);

 echo $parsed_results[0]['result']['metadata']['globalCounts']['count'];

?>

我尝试了一切,我已经坐了3个小时,但可以让它发挥作用。 但它似乎对他非常好。这是完全直截了当的简单剧本。

我甚至使用firebug来检查请求。我尝试用找到的一个替换post数据值。

[{"method":"pos.plusones.get","id":"pos.plusones.get","params":{"cdx":"cb4","id":"http://www.tomanthony.co.uk/google_plus_one_api_example.php","source":"widget","container":"http://www.tomanthony.co.uk/google_plus_one_api_example.php","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"pos.plusones.get","apiVersion":"v1"}]

我不知道我哪里错了。这只是一个简单的代码。

2 个答案:

答案 0 :(得分:6)

curl不接受服务器的CA可能存在问题。您可以通过以下方式找到答案:

$curl_results = curl_exec ($ch);
echo curl_error($ch);

如果确实存在不受信任的CA问题,您有两种选择。不安全和简单的方法是添加一个卷曲选项:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

这会禁用检查。第二个选项(更好,更复杂)是转到https://clients6.google.com并导出CA证书并将其提供给卷曲,如下所示:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/exported.crt");

答案 1 :(得分:2)

此处其他帖子中列出的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;
}