所以,我早上大部分时间都在研究网站上定制的社交分享按钮。读取Twitter和Facebook的共享信息没有问题,但Google Plus是一个真正的挑战,因为它们不提供易于使用的GET API。
我发现了一种使用裸骨卷曲的工作技术(http://www.tomanthony.co.uk/blog/google_plus_one_button_seo_count_api/comment-page-1/ ). 但之前,我曾努力让它与Wordpress的功能(如wp\\u remote\\u post)配合使用,但失败了。谁能告诉我我做错了什么?
以下是我设法聚在一起的内容。两个不同的请求失败:
$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\');
}
附言:API密钥是开发人员的公共密钥。
SO网友:Deniz Fuchidzhiev
我知道这个问题已经2年了,但我花了两天的时间努力找到了同一个问题的解决方案,这是我的代码(对我来说,它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\';
}
与上面代码的最大区别是,我以JSON格式发送请求,而不是以数组的形式发送。