2

So I've spent most of my morning working on my website's customized social share buttons. Reading out Twitter and Facebook shares were no problem, but Google Plus is a real challenge, as they don't offer an easy-to-use GET API.

I found a working technique using barebones CURL ( http://www.tomanthony.co.uk/blog/google_plus_one_button_seo_count_api/comment-page-1/ ). But previously, I've tried hard to get it to work with Wordpress' functions like wp_remote_post and failed. Anyone can tell me what I've been doing wrong?

Here is what I managed to get together. Two different requests fail:

$google_url = 'https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ';
$headers    = array('Content-type' => 'application/json');
$body       = array( "method"   => "pos.plusones.get"
                    ,"id"       => "p"
                    ,"params"   => array(   "nolog"     => true
                                            ,"id"       => $url
                                            ,"source"   => "widget"
                                            ,"userId"   => "@viewer"
                                            ,"groupId"  => "@self"
                                        )
                    ,"jsonrpc"  =>"2.0"
                    ,"key"      => "p"
                    ,"apiVersion" => "v1"
                );

$response = wp_remote_post( $google_url , array( 'method'   => 'POST'
                                                ,'headers'  => $headers
                                                ,'body'     => $body    ) );

if (!is_wp_error($response)) {
    $resp = json_decode($response['body'],true);
    _e($response['body']);
}
else
{
    _e('error');
}
// Another attempt to get data with WP_Http 
$request = new WP_Http;
$result = $request->request( $google_url , array( 'method' => 'POST', 'body' => $body, 'headers' => $headers ) );

if (!is_wp_error($result)) {
    $resp = json_decode($result['body'],true);
    _e($result['body']);
}
else
{
    _e('error AGAIN');
}

PS: The API key is a public one for developers.

0

1 Answer 1

3

I see that the question is 2 years old, but I worked hard two days to find solution of the very same problem and here is my code (for me works 100%).

$json_string = wp_remote_request('https://clients6.google.com/rpc',
array(
    'method'    => 'POST',
    'body'      => '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.rawurldecode(get_permalink()).'","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]',
    'headers' => array('Content-Type' => 'application/json')
));

$json = json_decode($json_string['body'], true);



if(isset($json[0]['result']['metadata']['globalCounts']['count'])){
   echo intval($json[0]['result']['metadata']['globalCounts']['count']);
}else{
   echo'0';
}

The big difference from the code above is that I send request in JSON format, not as an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.