58

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?

budi
  • 6,351
  • 10
  • 55
  • 80
Vijay Boyapati
  • 7,632
  • 7
  • 31
  • 48

13 Answers13

48

UPDATE: This solution is no longer valid. FQLs are deprecated since August 7th, 2016.


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

Also http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.techlila.com will show you all the data like 'Share Count', 'Like Count' and 'Comment Count' and total of all these.

Change the URL (i.e. http://www.techlila.com) as per your need.

This is the correct URL, I'm getting right results.

EDIT (May 2017): as of v2.9 you can make a graph API call where ID is the URL and select the 'engagement' field, below is a link with the example from the graph explorer.

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

Rajesh Namase
  • 597
  • 3
  • 6
  • 10
    It is possible to get same results in JSON format: https://graph.facebook.com/fql?q=select%20%20like_count%20from%20link_stat%20where%20url=%22http://yoast.com%22 – Vladimir Dec 07 '12 at 20:25
  • 1
    @Vladimir Is that URL part of the official API, or will it be deprecated? Seems like you don't need an API key to use it.. – Henley Dec 18 '12 at 21:09
  • 2
    Yes, it is part of the official API. This request uses Facebook Query Language (FQL) http://developers.facebook.com/docs/technical-guides/fql/ – Vladimir Dec 19 '12 at 17:59
  • 3
    FQL is indeed deprecated as of API 2.1+. Check vincentlcy's answer below or http://stackoverflow.com/questions/4764562/is-it-possible-to-get-the-number-of-facebook-likes-for-a-url/5899343#5899343 – Aron Jan 05 '15 at 15:34
  • I have tried this method to get like counts for various pages on Facebook. In most cases it works fine, but there are several that just return 0. Any idea why? Could there be a privacy setting that the page owner changes so that the like count is not shown? – Wonko the Sane Feb 21 '16 at 16:03
  • You can use a comma separated list of URL's to get a list of results http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.almaruf.com,http://yahoo.com,http://google.com – almaruf Aug 03 '16 at 09:15
  • 1
    As of August 8, 2016, FQL will no longer be available and cannot be queried https://developers.facebook.com/docs/reference/fql/ – Murhaf Sousli Aug 05 '16 at 13:46
  • 1
    This answer is no longer appropriate as FB has done away with FQL. – Krishnan Venkiteswaran Jan 30 '17 at 03:24
42

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

budi
  • 6,351
  • 10
  • 55
  • 80
17

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"
}
John Culviner
  • 22,235
  • 6
  • 55
  • 51
11

All previous answers have since been deprecated. This method works as of Aug 2016:


To get the like count of any URL:

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

Then grab shares->share_count from the returned JSON object.


Fan count for a Facebook page:

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

Then grab the 'fan_count' field from the returned JSON object.


You can test this out and get your access token using the Graph API Explorer

jetlej
  • 3,382
  • 6
  • 29
  • 41
7

Facebook Graph is awesome. Just do something like below. I've entereted perl.org URL, you can put any URL there.

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

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • 1
    This is the best answer, straight and to the point. The endpoint returns a simple JSON you can parse pretty much from anywhere and you don't even have to authenticate. Perfect! – sergserg Apr 05 '15 at 21:23
  • 2
    This isn't working anymore (`(#12) fql is deprecated for versions v2.1 and higher`) – Quentin S. Aug 18 '16 at 19:06
7

For latest 2.1 Graph API, an example to get likes for imdb.com will be

Using this to get the id https://developers.facebook.com/tools/explorer/?method=GET&path=%3Fid%3Dhttp%253A%252F%252Fwww.imdb.com%3Ffields%3Dlikes&version=v2.1

and then get the likes

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.

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

vincentlcy
  • 1,157
  • 2
  • 17
  • 19
  • 1
    This doesn't return the number of likes but the likes name and id with pagination. – e666 Sep 14 '16 at 12:44
  • To have the total number of likes you need to add `?summary=true`. Found after long search on Facebook doc : https://developers.facebook.com/docs/graph-api/reference/v2.7/object/likes – e666 Sep 14 '16 at 13:21
4

You Can Show Facebook Share/Like Count Like This: ( Tested and Verified)

$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];
Atif Tariq
  • 2,650
  • 27
  • 34
3

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

Community
  • 1
  • 1
Edward Fung
  • 426
  • 8
  • 16
2

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>
varun
  • 768
  • 7
  • 19
1

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

reylimjr
  • 361
  • 8
  • 16
1

You need the extended permission "read_stream", then you need to call the Facebook API endpoint, and add likes,shares to your fields.

This call

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

will return a data array like this

{
   "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"
    }
   ]
}
Beryllium
  • 12,808
  • 10
  • 56
  • 86
Abood Sy
  • 282
  • 3
  • 10
1

Your question is quite old and Facebook has depreciated FQL now but what you want can still be done using this utility: Facebook Analytics. However you will find that if you want details about who is liking or commenting it will take a long time to get. This is because Facebook only gives a very small chunk of data at a time and a lot of paging is required in order to get everything.

Loïc Joachim
  • 89
  • 1
  • 8
0

I don't think Facebook's Open Graph Object i.e. "og_object" provides anything more than comment_count & share_count for a URL. Try this; replace $YOUR_URL with the URL and $ACCESS_TOKEN with your access token in the below link https://graph.facebook.com/v2.5/$YOUR_URL?access_token=$ACCESS_TOKEN

For example:

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"
}

Also, if you try to get likes, you would get the following error 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"
  }
}
Chenna V
  • 10,185
  • 11
  • 77
  • 104
  • `?id=#{url}&fields=og_object{engagement},share` the number of likes you can get from the engagement object see https://developers.facebook.com/docs/graph-api/reference/v2.5/url/ – Mihai Mar 15 '16 at 06:51
  • How do I get a long-term access-token? – Werner Aug 19 '16 at 07:05
  • @Werner the access token is the APP_ID|APP_SECRET . Take a look at the last step in this https://smashballoon.com/custom-facebook-feed/access-token/ – Chenna V Aug 19 '16 at 16:29