Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.