让Facebook喜欢/分享给定URL的数量

时间:2012-03-15 21:15:27

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

我正在使用Facebook API获取给定网址的类似/共享计数。奇怪的是,返回结果似乎很不一致。例如,此页面返回结果:

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-sunset-kayaking-hot-chowder'&format=json

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

有关API可能为某些网址而不是其他网址做出响应的原因的任何线索?

13 个答案:

答案 0 :(得分:47)

更新: 此解决方案不再有效。 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会向您显示所有数据,例如“分享计数”,“赞计数”和“评论计数”以及所有这些数据的总和。

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

这是正确的网址,我的结果是正确的。

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

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

答案 1 :(得分:42)

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

更新10/2017(v2.10):

这是获取给定网址和共享计数的不推荐方式(无需访问令牌):

  

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

答案 2 :(得分:17)

使用开放图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"
}

答案 3 :(得分:11)

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

要获得任意网址的计数:

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

然后从返回的JSON对象中获取shares-&gt; share_count。

Facebook页面的粉丝数:

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

然后抓住&#39; fan_count&#39;返回的JSON对象中的字段。

您可以使用Graph API Explorer

对此进行测试并获取访问令牌

答案 4 :(得分:7)

Facebook Graph非常棒。只需做下面的事情。我已经输入了perl.org网址,您可以在那里放置任何网址。

https://graph.facebook.com/fql?q=SELECT%20like_count,%20total_count,%20share_count,%20click_count,%20comment_count%20FROM%20link_stat%20WHERE%20url%20=%20%27http://perl.org%27

答案 5 :(得分:7)

对于最新的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/

答案 6 :(得分:4)

你可以显示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];

答案 7 :(得分:3)

对于图API 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 :(得分:2)

使用以下网址并将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>

答案 9 :(得分:1)

我看到这个很好的教程,介绍如何使用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

答案 10 :(得分:1)

您需要扩展权限“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"
    }
   ]
}

答案 11 :(得分:1)

你的问题已经很久了,Facebook现在已经贬低了FQL,但你仍然可以使用这个实用程序来完成你的工作:Facebook Analytics。但是你会发现,如果你想要了解谁喜欢或评论它的细节需要很长时间才能获得。这是因为Facebook一次只提供非常小的数据块,并且需要大量的分页才能获得所有内容。

答案 12 :(得分:0)

我不会想到Facebook的开放图形对象,即&#34; og_object&#34;提供的不仅仅是comment_count&amp; share_count用于URL。 试试这个;在下面的链接中使用您的访问令牌替换$ YOUR_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"
  }
}
相关问题