如何获取 google plus +1 按钮的计数器?

发布于 2024-12-04 11:57:00 字数 392 浏览 0 评论 0原文

我已在网站上添加了一个 google +1 按钮,但我想获取它的计数器,以便我可以对其进行一些数学计算。是否可以输入通过创建 +1 按钮的标准方法创建的 iframe,或者我需要进行一些调整吗?

<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<g:plusone></g:plusone>

我尝试过此链接:1 ,但这不是很准确

I have added a google +1 button to a website, but I want to get it's counter so i can do some math over it. is it possible to enter the iframe created by the standard method of creating the +1 button or do I need to make some adjustment?

<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<g:plusone></g:plusone>

I've tried this link:1 , but this is not very accurate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥繁 2024-12-11 11:57:00

如果您可以访问curl/file_get_contents/readfile/wget 或某种方式来获取外部URL,这非常简单。

加载以下网址:https://plusone.google.com/_/+1/fastbutton?url=URLENCODED_URI已更新网址,请参阅下面的注释*

URLENCODED_URI 为您希望了解 +1 数量的网站,例如 http://www.google.com (http%3A%2F%2Fwww.google.com)

例如,获取 URI https://plusone.google.com/_/+1/fastbutton?url=http://www.google.com / (UPDATED URI) 并找到第一次出现的 window.__SSR = {'c': 32414.0 ,'si'。最好使用正则表达式,但我将把实现留给您和您选择的编程语言(服务器端或客户端)。

'c' 后面的浮点数是该站点拥有的 +1 的数量。对于 google.com,这是 32,414。不用担心浮点数,您可以安全地将其转换为整数。

* 更新: URL 已更新,旧 URL 开始为 404。请记住,这是预期的,因为这是非官方方法。目前还没有官方方法。

If you can access curl/file_get_contents/readfile/wget or some way to fetch an external URL, this is quite simple.

Load the following URL: https://plusone.google.com/_/+1/fastbutton?url=URLENCODED_URI (UPDATED URL, see note below *)

URLENCODED_URI is the site you wish to know the number of +1's for, e.g. http://www.google.com (http%3A%2F%2Fwww.google.com)

For example, fetch the URI https://plusone.google.com/_/+1/fastbutton?url=http://www.google.com/ (UPDATED URI) and locate the first occurance of window.__SSR = {'c': 32414.0 ,'si'. Preferably use regexp for this, but I'll leave the implementation to you and your chosen programming language (server side or client side).

The float number following 'c' is the number of +1's the site have. For google.com this is 32,414. Don't worry about the float, you can safely convert it to an integer.

* UPDATE: The URL has been updated as the old URL started to 404. Remember, this is expected as this is an unofficial method. There exist no official method (yet).

瀞厅☆埖开 2024-12-11 11:57:00

您可以使用回调函数来获取显示计数的 div 的值吗?

function count() {
    var count = $('#aggregateCount').html();
}

    <g:plusone callback="count()"></g:plusone>

我基于气泡注释按钮,我还没有测试过它但这样的事情应该有效。

Could you use a callback function to grab the value of the div that displays the count?

function count() {
    var count = $('#aggregateCount').html();
}

    <g:plusone callback="count()"></g:plusone>

I'm basing this off the bubble annotation button, I haven't tested it but something like this should work.

墨离汐 2024-12-11 11:57:00

适合我获取 Google Plus 计数器的纯客户端解决方案如下。它不需要 API 密钥。

var url = "http://www.yoursite-to-be-counted.com";
var data = {
    "method":"pos.plusones.get",
    "id": url,
    "params":{
        "nolog":true,
        "id": url,
        "source":"widget",
        "userId":"@viewer",
        "groupId":"@self"
    },
    "jsonrpc":"2.0",
    "key":"p",
    "apiVersion":"v1"
  };
  $.ajax({
    type: "POST",
    url: "https://clients6.google.com/rpc",
    processData: true,
    contentType: 'application/json',
    data: JSON.stringify(data),
    success: function(r){
      setCount($(".google-plus-count"), r.result.metadata.globalCounts.count);
    }
  });

  var setCount = function($item, count) {
    if (count) {
      $item.text(count);
    }
  };

然后我有一些带有积分的html

<div class="google-plus-count"></div>

,这里转到这个答案。

A pure client-side solution that works for me to get the Google Plus counter is as follows. It does not need an API key.

var url = "http://www.yoursite-to-be-counted.com";
var data = {
    "method":"pos.plusones.get",
    "id": url,
    "params":{
        "nolog":true,
        "id": url,
        "source":"widget",
        "userId":"@viewer",
        "groupId":"@self"
    },
    "jsonrpc":"2.0",
    "key":"p",
    "apiVersion":"v1"
  };
  $.ajax({
    type: "POST",
    url: "https://clients6.google.com/rpc",
    processData: true,
    contentType: 'application/json',
    data: JSON.stringify(data),
    success: function(r){
      setCount($(".google-plus-count"), r.result.metadata.globalCounts.count);
    }
  });

  var setCount = function($item, count) {
    if (count) {
      $item.text(count);
    }
  };

Then I have some html with

<div class="google-plus-count"></div>

Credits here goes to this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文