【问题标题】:Getting the Facebook like/share count for a given URL获取给定 URL 的 Facebook 喜欢/分享计数
【发布时间】:2012-04-01 11:10:31
【问题描述】:

我正在使用 Facebook API 来获取给定 URL 的点赞/分享数。奇怪的是,返回结果似乎很不一致。比如这个页面返回结果:

https://api.facebook.com/method/fql.query?query=select%20total_count,like_count,comment_count,share_count,click_count%20from%20link_stat%20where%20url='http://www.groupon.com/deals /seattlehelitourscom-by-classic-helicopter-corp'&format=json

然而,这个没有:

https://api.facebook.com/method/fql.query?query=select%20total_count,like_count,comment_count,share_count,click_count%20from%20link_stat%20where%20url='http://www.livingsocial.com/deals /278194-日落皮划艇-热杂烩'&format=json

第二页显然有一个共享计数,当我检查页面的 HTML 时,用于共享的 URL 是我在上面的 API 请求中放置的 URL。但是,API 不会响应任何点赞数或分享数的计数信息。

关于为什么 API 可能会响应某些 URL 而不会响应其他 URL 的任何线索?

【问题讨论】:

  • 似乎 Rest API 已被弃用。对此有何猜测?

标签: facebook facebook-graph-api facebook-fql


【解决方案1】:

更新: 此解决方案不再有效。 FQL 自 2016 年 8 月 7 日起已弃用。


https://api.facebook.com/method/fql.query?query=select%20%20like_count%20from%20link_stat%20where%20url=%22http://www.techlila.com%22

http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.techlila.com 还会向您显示所有数据,例如“分享数”、“点赞数”和“评论数”以及所有这些数据的总和。

根据需要更改 URL(即http://www.techlila.com)。

这是正确的 URL,我得到了正确的结果。

编辑(2017 年 5 月):从 v2.9 开始,您可以进行图形 API 调用,其中 ID 是 URL 并选择“参与”字段,下面是图形资源管理器中示例的链接。

https://developers.facebook.com/tools/explorer/?method=GET&path=%3Fid%3Dhttp%3A%2F%2Fcomunidade.edp.pt%26fields%3Dengagement&version=v2.9

【讨论】:

【解决方案2】:

我看到了这篇关于如何使用 PHP 从 facebook 获取点赞数的精彩教程。

public static function get_the_fb_like( $url = '' ){
 $pageURL = 'http://nextopics.com';

 $url = ($url == '' ) ? $pageURL : $url; // setting a value in $url variable

 $params = 'select comment_count, share_count, like_count from link_stat where url = "'.$url.'"';
 $component = urlencode( $params );
 $url = 'http://graph.facebook.com/fql?q='.$component;
 $fbLIkeAndSahre = json_decode( $this->file_get_content_curl( $url ) ); 
 $getFbStatus = $fbLIkeAndSahre->data['0'];
 return $getFbStatus->like_count;
}

这是一个示例代码。我不知道如何将格式正确的代码粘贴到此处,因此请访问此链接以更好地查看代码。

Creating a Custom Facebook like Counter

【讨论】:

    【解决方案3】:

    使用开放图形 API。这是一个查询“可口可乐”有多少赞的实时示例。

    https://developers.facebook.com/tools/explorer/?method=GET&path=cocacola%3Ffields%3Dlikes

    归结为:

    https://graph.facebook.com/cocacola?fields=likes

    你可以在 AJAX GET 中做什么

    结果是:

    {
      "likes": 71717854, 
      "id": "40796308305"
    }
    

    【讨论】:

    • 请求此资源需要访问令牌。
    • 使用第一个链接并获取访问令牌
    • 从 v2.6 开始,这不会返回喜欢的数量,而是返回喜欢的名称和 id 分页。
    • 问题是关于 URL,但您的答案是关于 Facebook 页面。
    【解决方案4】:

    您需要扩展权限“read_stream”,然后您需要调用 Facebook API 端点,并将likes,shares 添加到您的fields

    这个电话

    https://developers.facebook.com/tools/explorer?method=GET&path=me/feed?fields=likes,shares
    

    会返回一个像这样的数据数组

    {
       "data": [
        {
         "likes": {
            "data": [
                     {
                       "name": "name of user who liked status ",
                       "id": "id of user who liked status "
                     }
                    ],
            "count": number of likes
         },
         "shares": {
          "count": number of shares 
         }, 
         "id": "post id",
         "created_time": "post creation time"
        }
       ]
    }
    

    【讨论】:

      【解决方案5】:

      【讨论】:

      • 这是最好的答案,直截了当。端点返回一个简单的 JSON,您几乎可以从任何地方解析,甚至不必进行身份验证。完美!
      • 这行不通了 ((#12) fql is deprecated for versions v2.1 and higher)
      【解决方案6】:

      对于最新的 2.1 Graph API,获取 imdb.com 的赞的示例为

      使用它来获取 id https://developers.facebook.com/tools/explorer/?method=GET&path=%3Fid%3Dhttp%253A%252F%252Fwww.imdb.com%3Ffields%3Dlikes&version=v2.1

      然后获得赞

      https://developers.facebook.com/tools/explorer/?method=GET&path=414652589771%2Flikes&version=v2.1

      Document

      URL /?id={url}
      
      Represents an external URL as it relates to the Facebook social graph - shares and comments from the URL on Facebook, and any Open Graph objects associated with the URL.
      

      参考 http://harshtechtalk.com/how-get-likes-count-posts-comments-facebook-graph-api/

      【讨论】:

      【解决方案7】:

      对于图形 API v2.1,您可以仅使用 1 次调用获得点赞数,因此无需进行分页。

      例如获取http://www.imdb.com的点赞数

      https://graph.facebook.com/414652589771/likes?summary=1

      图形 API 浏览器 https://developers.facebook.com/tools/explorer/?method=GET&path=414652589771%2Flikes%3Fsummary%3D1&version=v2.1

      不知何故没有记录(至少在我提交这个答案的那一刻......)。 我在https://stackoverflow.com/a/18198957/1822624找到了答案

      【讨论】:

        【解决方案8】:

        使用下面的网址并将 myurl 替换为您的帖子网址,您将获得所有内容

        http://api.facebook.com/restserver.php?method=links.getStats&urls=myurl

        但请记住,它只会以 XML 格式为您提供响应

        例子:

        <share_count>1</share_count>
        <like_count>8</like_count>
        <comment_count>0</comment_count>
        <total_count>9</total_count>
        <click_count>0</click_count>
        <comments_fbid>**************</comments_fbid>
        <commentsbox_count>0</commentsbox_count>
        

        【讨论】:

        • 添加url参数&amp;format=json获取json格式。
        【解决方案9】:

        我不认为 Facebook 的开放图形对象(即“og_object”)为 URL 提供的不仅仅是 comment_count 和 share_count。 尝试这个;在下面的链接中,将 $YOUR_URL 替换为 URL,将 $ACCESS_TOKEN 替换为您的访问令牌 https://graph.facebook.com/v2.5/$YOUR_URL?access_token=$ACCESS_TOKEN

        例如:

        https://graph.facebook.com/v2.5/http://espn.go.com/nfl/story/_/id/14424066/handing-holiday-gifts-all-32-nfl-teams-nfl?access_token=$ACCESS_TOKEN

        {
          og_object: {
            id: "956517601094822",
            description: "Naughty or nice, every NFL team deserves something for Christmas. So in lieu of Santa Claus, Bill Barnwell is here to distribute some gifts.",
            title: "Barnwell: Handing out holiday gifts to all 32 teams",
            type: "article",
            updated_time: "2015-12-23T17:20:55+0000",
            url: "http://espn.go.com/nfl/story/_/id/14424066"
          },
          share: {
            comment_count: 0,
            share_count: 354
          },
          id: "http://espn.go.com/nfl/story/_/id/14424066/handing-holiday-gifts-all-32-nfl-teams-nfl"
        }
        

        另外,如果您尝试获得喜欢,您会收到以下错误 https://graph.facebook.com/http://rottentomatoes.com?fields=likes&summary=1&access_token=$ACCESS_TOKEN

        {
          error: {
            message: "(#100) Tried accessing nonexisting field (likes) on node type (URL)",
            type: "OAuthException",
            code: 100,
            fbtrace_id: "H+KksDn+mCf"
          }
        }
        

        【讨论】:

        【解决方案10】:

        您可以像这样显示 Facebook 分享/喜欢计数:(经过测试和验证)

        $url = http://www.yourdomainname.com // You can use inner pages
        
        $rest_url = "http://api.facebook.com/restserver.php?format=json&method=links.getStats&urls=".urlencode($url);
        
        $json = json_decode(file_get_contents($rest_url),true);
        
        
        echo Facebook Shares = '.$json[0][share_count];
        
        echo Facebook Likes = '.$json[0][like_count];
        
        echo Facebook Comments = '.$json[0][comment_count];
        

        【讨论】:

        【解决方案11】:

        您的问题已经很老了,Facebook 现在已经贬低了 FQL,但您仍然可以使用此实用程序完成您想要的:Facebook Analytics。但是,您会发现,如果您想要有关谁喜欢或评论的详细信息,则需要很长时间才能获得。这是因为 Facebook 一次只提供很小的数据块,并且需要大量的分页才能获取所有内容。

        【讨论】:

          【解决方案12】:

          所有以前的答案都已被弃用。此方法自 2016 年 8 月起有效:


          要获取任何 URL 的点赞数:

          GET 请求:https://graph.facebook.com/[url]/access_token=[access_token]

          然后从返回的 JSON 对象中获取 share->share_count。


          Facebook 页面的粉丝数:

          GET 请求:https://graph.facebook.com/[url]/?fields=fan_count&access_token=[access_token]

          然后从返回的 JSON 对象中获取“fan_count”字段。


          您可以对此进行测试并使用Graph API Explorer 获取您的访问令牌

          【讨论】:

          • 对于 URL,shares_count 返回分享数 + 点赞数。
          【解决方案13】:

          As of August 8th, 2016, FQLs are deprecated.


          更新 10/2017 (v2.10):

          这是获取给定 URL 的点赞数和分享数(不需要访问令牌)的一种不被弃用的方法:

          https://graph.facebook.com/?fields=og_object{likes.summary(total_count).limit(0)},share&id=https://www.stackoverflow.com


          结果:

          {
             "og_object": {
                "likes": {
                   "data": [
          
                   ],
                   "summary": {
                      "total_count": 83
                   }
                },
                "id": "10151023731873397"
             },
             "share": {
                "comment_count": 0,
                "share_count": 2915
             },
             "id": "https://www.stackoverflow.com"
          }
          

          JQuery 示例:

          $.get('https://graph.facebook.com/'
              + '?fields=og_object{likes.summary(total_count).limit(0)},share&id='
              + url-goes-here,
              function (data) {
                  if (data) {
                      var like_count = data.og_object.likes.summary.total_count;
                      var share_count = data.share.share_count;
                  }
              });
          

          参考:

          https://developers.facebook.com/docs/graph-api/reference/url

          【讨论】:

          • 如果共享不起作用,请尝试使用“共享”。对我来说,“股份”起作用了。快乐编码:)
          • 我应该使用 access_token 吗?
          • @AntonIpatov,不需要访问令牌。
          • 可能2周后就不行了,响应码(#2)服务暂时不可用……有人知道怎么办了吗?
          猜你喜欢
          • 2012-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多