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.
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%).
The big difference from the code above is that I send request in JSON format, not as an array.