如何在Javascript中获取当前页面的Google +1数量?

时间:2014-03-25 17:57:00

标签: javascript google-plus

我想获得当前网页的Google + 1s数量?我想在Javascript中执行此过程,然后将数量的共享或+ 1s写入数据库。这就是为什么,我需要它。那么,我怎样才能在PHP中完成这个过程(获得+ 1s的数量)?

1 个答案:

答案 0 :(得分:0)

您可以使用official Google+ documentation了解如何使用计数添加+1按钮。它还有一个代码生成器,可以为您生成JavaScript代码。

要通过PHP获取这些值(在数据库中写入+ 1的数量),可以使用以下代码在PHP中使用cURL:

function get_plusones($url) {
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, 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($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  $curl_results = curl_exec ($curl);
  curl_close ($curl);
  $json = json_decode($curl_results, true);
  return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
}

代码来自previous stackoverflow answer