Difficulty getting google plus one count
Asked Answered
G

2

2

I have been searching all over the internet on how to get programaticaly google plus one button count. Finally i found this article Here is the Php Script mentioned in the arcticle.

<?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'];

?>

I tried everything,i have been sitting on it for 3 hours but could get it to work. But it seems to work for him perfectly fine.It is perfectly straight forward and simple script.

I even used firebug to examine the requests.I tried substituting the post data value with one i found .

[{"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"}]

I dont have a clue where am i going wrong.Its just a simple code.

Giesecke answered 6/9, 2011 at 14:3 Comment(8)
You should remove your API key from your sample code.Vacuous
It is a developerKey that is the same for everyone. Changing it will give you a 400 error.Giesecke
Have you done a var_dump($curl_results) to see what's coming back?Damn
@Marc When i used var_dump($curl_results) I got boolean falseGiesecke
That means your curl_exec() call is failing. Do a echo curl_error($ch) after calling curl_exec to retrieve the error message. In general, NEVER assume a call to an external service succeeded, as you are. Always check for error conditions. Network glitches happen far too often to NOT check for them.Damn
@Marc I used echo curl_error($ch) and i got this SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failedGiesecke
@Marc curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); Got it solved by adding above lines.Thank You very much. :)Giesecke
You should add the cURL error and the solution as an answer and mark it accepted.Vacuous
A
6

Probably a problem with curl not accepting the CA of the server. You can find out for sure with:

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

If it is indeed a problem with the untrusted CA, you have two options. The insecure and easy way would be to add one more option to curl:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

This disables the check. The second option (better and a bit more complicated) would be to go to https://clients6.google.com and export the CA certificate and feed it to curl like so:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/exported.crt");
Ardine answered 6/9, 2011 at 14:36 Comment(0)
P
2

The cURL and API way listed in the other posts here no longer works.

There is still at least 1 method, but it's ugly and Google clearly doesn't support it. You just rip the variable out of the JavaScript source code for the official button with a regular expression:

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;
}
Potsdam answered 16/3, 2013 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.