Getting the Facebook like/share count for a given

2019-01-07 02:24发布

I'm using the Facebook API to get the like/share count for given URLs. The strange thing is that it seems to be quite inconsistent in returning results. For example, this page returns results:

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

Whereas, this one does not:

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

The second page clearly has a share count on it, and when I inspect the HTML of the page, the URL which is being used to share is the one I've placed into the API request above. However, the API does not respond with any count information for either number of likes or shares.

Any clues on why the API might be responding for some URLs but not for others?

13条回答
萌系小妹纸
2楼-- · 2019-01-07 03:00
老娘就宠你
3楼-- · 2019-01-07 03:00

For graph API v2.1, you can get the like count using 1 call only and, therefore, no need to go through the paging.

For example, to get the number of likes of http://www.imdb.com

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

Graph API Explorer https://developers.facebook.com/tools/explorer/?method=GET&path=414652589771%2Flikes%3Fsummary%3D1&version=v2.1

It is somehow not documented (at least at the moment I submit this answer...). I found the answer in https://stackoverflow.com/a/18198957/1822624

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-07 03:02

use below URL and replace myurl with your post url and you will get all the things

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

but keep in mind it will give you response in XML format only

Example :

<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>
查看更多
等我变得足够好
5楼-- · 2019-01-07 03:03

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


Update 10/2017 (v2.10):

Here's a non-deprecated way to get a given URL's like and share count (no access token required):

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


Result:

{
   "og_object": {
      "likes": {
         "data": [

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

JQuery Example:

$.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;
        }
    });

Reference:

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

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-07 03:04

Use the open graph API. Here is a live example querying how many likes "Coca Cola" has.

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

Which boils down to:

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

Which you could do in an AJAX GET

The result is:

{
  "likes": 71717854, 
  "id": "40796308305"
}
查看更多
看我几分像从前
7楼-- · 2019-01-07 03:08

I see this nice tutorial on how to get the like count from facebook using 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;
}

here is a sample code.. I don't know how to paste the code with correct format in here, so just kindly visit this link for better view of the code.

Creating a Custom Facebook like Counter

查看更多
登录 后发表回答