[facebook] Getting the Facebook like/share count for a given URL

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?

This question is related to facebook facebook-graph-api facebook-fql

The answer is


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


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


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>


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

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

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


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];

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

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.


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


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


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/


Examples related to facebook

I am receiving warning in Facebook Application using PHP SDK React-Native: Application has not been registered error Can't Load URL: The domain of this URL isn't included in the app's domains Facebook OAuth "The domain of this URL isn't included in the app's domain" Facebook login message: "URL Blocked: This redirect failed because the redirect URI is not whitelisted in the app’s Client OAuth Settings." Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView` Open Facebook Page in Facebook App (if installed) on Android App not setup: This app is still in development mode IOS - How to segue programmatically using swift Get ALL User Friends Using Facebook Graph API - Android

Examples related to facebook-graph-api

"Uncaught (in promise) undefined" error when using with=location in Facebook Graph API query Facebook OAuth "The domain of this URL isn't included in the app's domain" Facebook login message: "URL Blocked: This redirect failed because the redirect URI is not whitelisted in the app’s Client OAuth Settings." Facebook Graph API v2.0+ - /me/friends returns empty, or only friends who also use my application Creating a Facebook share button with customized url, title and image Facebook API "This app is in development mode" The developers of this app have not set up this app properly for Facebook Login? New og:image size for Facebook share? facebook: permanent Page Access Token? How to programmatically log out from Facebook SDK 3.0 without using Facebook login/logout button?

Examples related to facebook-fql

Getting the Facebook like/share count for a given URL EOFError: end of file reached issue with Net::HTTP