2012-03-15 43 views
55

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

 
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请求中放置的那个。但是,API不会响应任何数量的喜欢或份额的计数信息。

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

+1

它似乎REST API已被弃用。有关这个的任何猜测? – Elangovan 2016-09-06 12:19:05

回答

48

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


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月):从版本2.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

+10

以JSON格式可以得到相同的结果:https://graph.facebook.com/fql?q=select%20%20like_count%20from%20link_stat%20where%20url=%22http://yoast.com%22 – Vladimir 2012-12-07 20:25:27

+1

@Vladimir这是官方API的URL部分,还是会被弃用?似乎你不需要API密钥来使用它.. – 2012-12-18 21:09:00

+2

是的,它是官方API的一部分。此请求使用Facebook查询语言(FQL)http://developers.facebook.com/docs/technical-guides/fql/ – Vladimir 2012-12-19 17:59:54

1

我看看如何获​​得来自Facebook使用PHP等等计数这个漂亮的教程。

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

17

使用开放图API。这里有一个生动的例子,询问有多少人喜欢“可口可乐”。

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

这可以归结为:

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

,你可以在一个AJAX做得到

结果是:

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

需要访问令牌来请求此资源。 – 2015-06-20 05:27:06

+0

使用第一个链接并获取访问令牌 – 2015-06-29 18:28:49

+0

从v2.6开始,这不会返回喜欢的数字,而是会返回喜欢的名称和ID与分页。 – e666 2016-09-14 12:24:01

1

你需要扩展许可“read_stream”,t如果您需要致电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" 
    } 
    ] 
} 
7

Facebook的图形是真棒。只需做下面的事情。我输入了perl.org的URL,你可以在那里放置任何URL。

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

+1

这是最好的答案,直接和重点。该端点返回一个简单的JSON,您可以从任何地方进行分析,甚至不需要进行身份验证。完善! – sergserg 2015-04-05 21:23:27

+1

这不再起作用('(#12)fql已被弃用的版本v2.1和更高版本) – chteuchteu 2016-08-18 19:06:39

7

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

+1

这不会返回喜欢的数字,但喜欢的名字和id与分页。 – e666 2016-09-14 12:44:16

+0

要获得需要添加'?summary = true'的总数。在Facebook文档中搜索很长时间后发现:https://developers.facebook.com/docs/graph-api/reference/v2.7/object/likes – e666 2016-09-14 13:21:17

3

对于图形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

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> 
+1

添加url参数'&format = json'以获取json格式。 – Dave 2015-12-17 01:34:51

0

我不认为Facebook的Open GRA ph对象即“og_object”提供的内容超过comment_count & share_count的URL。 尝试一下;在下面的链接中用您的访问令牌替换$ 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" 
    } 
} 
+0

'?id =#{url}&fields = og_object {engagement},share'您可以从订婚对象获得的喜好数量请参阅https://developers.facebook.com/docs/graph-api/reference/v2。 5/url/ – Mihai 2016-03-15 06:51:44

+0

如何获得长期访问令牌? – Werner 2016-08-19 07:05:23

+0

@Werner访问令牌是APP_ID | APP_SECRET。看看这个https:// smashballoon的最后一步。com/custom-facebook-feed/access-token/ – blueskin 2016-08-19 16:29:24

4

您可以显示Facebook分享/ Like Count Like This:(测试和确认)

$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]; 
+1

此方法现在已被弃用,不再可用 – jetlej 2016-08-18 19:32:57

+0

jetlej您能否更新新方法或链接? – 2016-08-19 08:13:04

+0

最后发现它:) http://stackoverflow.com/questions/29702192/request-to-get-total-count-of-facebook-page-likes-in-v2-3-api – jetlej 2016-08-20 14:15:46

1

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

10

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


要得到任何URL的像数:

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

然后抓住股份有关>从返回的JSON SHARE_COUNT目的。


风扇转数的Facebook页面:

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

然后从返回的JSON对象抢 'fan_count' 字段。


您可以使用Graph API Explorer

+3

对于URL的shares_count返回股数+喜欢的数量。 – e666 2016-09-14 14:20:09

33

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


更新二千零十七分之十(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

+0

如果共享不起作用,请尝试使用“份额”。对我而言,“股份”起作用。快乐编码:) – 2017-11-12 11:22:56