The last article showed how to add a Facbook “Like” button to your website and observing the click event on the button to track and/or do something else with JavaScript when a user clicks on the “Like” button.
Now you might want to get some information on the URL via some automated processes like cronjobs etc. You can simply use FQL - the Facebook Query Language - for that purpose. FQL is similar to SQL but doesn’t support all of the features.
Using FQL
Here is some FQL to get some statistics for a link:
SELECT like_count, total_count, share_count, click_count from link_stat where url="http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/"
You can simply use a Facebook API client SDK or access the data directly via the URL even in your browser:
This will return XML like this:
<?xml version="1.0" encoding="UTF-8"?> <fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true"> <link_stat> <like_count>4</like_count> <total_count>5</total_count> <share_count>1</share_count> <click_count>0</click_count> </link_stat> </fql_query_response>
This means you can also use something like cURL or even PHP’s file_get_contents() method to obtain this information and parse it with SimpleXML, DOM or even regular expressions.
Facebook doesn’t seem to define any specific API call limit but there seems to be a limit to the number of calls per day.
Pitfalls
The Facebook API doesn’t support “LIKE” queries in FQL so you cannot find out how many pages on your website were liked directly - only by querying like described above for every single URL on your website.
I hope this short article is useful for you. If you like it why not just click on the “Like” button below?
































Hi Sascha, that is very helpful information.
I have a question:
Are we limited to the count only?
Is there a way to know who (facebookID) liked what (url or xid)?
Just saw that you already mentioned the LIKE problem.
Also with substr it is not working
Like this … where substr(url,0,28) = “http://www.saschakimmel.com/”
https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20substr%28url,0,28%29%20=%20%22http://www.saschakimmel.com/%22
@Guvenc: Thanks for pointing this out, I actually tested this as well but as it didn’t work I didn’t care to mention it. Sorry if that lead you to spend time on researching it yourself.
thank you, I was looking for something to show who likes, but I got useful link, that is the count of sharing and Like. \ /0/
I’ll search for the fans list though
Thanks a lot.
Hi Sascha - I have found your article incredibly helpful - thank you so much.
Now I am sorry if this is a dumb question:
When I view the URL you supplied
https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/%22
I can see the results fine.
However - when I have tried to access it from scrips I get a blank result. EG I have tried simpleXML, curl, file_get_contents. I dont get errors as such… just seems like there is nothing retrurned. I have tested other XMLs, and they all load (although all are http rather than https). Is there something silly and simple I may have overlooked?
Thanks in advance
Mike
@Mike: Have you tried using http instead of https? It works as well. The only reason I see why it wouldn’t work might be a lack of (Open)SSL support in your PHP version or SSL certificate problems (especially with curl). However you should see an error or a warning. Have you made sure that errors are displayed?
ini_set('display_errors', true);error_reporting(E_ALL);
Hi Sascha: Thank you so much!!! That was the silly thing I overlooked. http worked just fine. I haven’t checked my open ssl support yet - but its likely off (new php installation).
Thanks again for the article, and your quick response!
This is great. Thank you. I am only wondering: What do the metrics mean? Can you help with that?
My first thoughts were:
- “like” count = How many people clicked on “like” (?)
- “share” count = How many people shared the url in facebook (where? In messages or just on their wall?).
- “total count = How many people clicked on “like” and shared (?) -> Makes sense in your example (4 likes + 1 share = 5), but I have pages (ex: http://www.e-fellows.net/show/detail.php/21049) with numbers like this:
like_count 22
total_count 84
share_count 30
=> 30 + 22 is not exactly 84…
- “click” count” = No idea
Do you have any more information on this?
Thanks!
Lukas
Sorry for another comment. I just checked another blog posting which gives very good advice on how to interpret these stats: http://canvoo.com/blog/21028/facebook-like-button-count-inaccuracies
Here the mystery of the “total count” is being solved: total count = likes + shares + comments
Your query doesn’t extract the comments, so the total count is misleading. The article above had 22 comments, so together with the 52, that’s exactly the 84 I was looking for.
The following URL btw is a little cleaner if you want to get the URL stats:
http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.e-fellows.net/show/detail.php/21049
Lukas, thanks for pointing this out.
So the answer might be “no”, judging from your article, but I will ask more explicitly:
Can you use FQL to determine what are the “top ten most Liked” pages on your website for a given period of time ?
I hope the answer is yes, this would be an excellent proxy for “this is my killer content”… if not, What A Whiff On The Behalf Of Facebook !!! C’mon Facebook, this is a critical stat. If it is yes, then hallelujah facebook. Thanks !
Hi,
I’m trying to get the same info using http://api.facebook.com/restserver.php?method=links.getStats&urls=http://danc.stagename.com/beatmaker/shared_track/26/120808811/ but all the stats are 0 event though I do have 2 people who have liked the content. I’m wondering if it’s because I have not submitted the app yet(still developing) or if there’s something else I’m missing.
I’ve also tried the solution from http://canvoo.com/blog/21028/facebook-like-button-count-inaccuracies where you get stats, esp the fan_count, by calling https://graph.facebook.com/http://danc.stagename.com/beatmaker/shared_track/26/120808811/ and all I get is the “id” field returned…
acutally..nevermind …it was the slash at the end…
Your article is awesome, I haven’t found anything as good as this on the subject, I just have one problem.
Is there anyway I could get the number of comment posted through the social plugin, the fql query doesn’t return any comments for my site, even though there are comments on the page.
Thanks
Marc
Hello Lukas,
https://api.facebook.com/method/fql.query?query=SELECT%20share_count,click_count,%20like_count,%20total_count%20FROM%20link_stat%20WHERE%20url=%22http://www.asendes.com%22
I was using this query to get the stats…everthng seems to be ok..but the click_count always returns me 0.do you have any idea?
Thanks in advance.
Sathish.
I have a Like button on my web page. Facebook Insights shows the Like count for the URL however there is no count shown next to the Like button on my web page. This is an example: http://this1that1whatever.com/. Does anyone have any ideas on what might be causing this misbehavior? Thanks.
there is one facebook page also which shows detailed statistics….i forgot the url
sorry
To get xml stuff happening in php.
Here is my little trick.
$somexml = simplexml_file_load(’https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/%22′);
then retrieve the data like
echo $somexml->chilnode->childnode
or $childnode1 = $somexml->chilnode->childnode
eg..
$like_count = $somexml->link_stat->like_count
echo $somexml->link_stat->like_count . ‘ people like this page’; //echo’s 4 people like this page
echo $like_count . ‘ people like this page’; //echo’s 4 people like this page
$sopmexml
to quote mike below
However - when I have tried to access it from scrips I get a blank result. EG I have tried simpleXML, curl, file_get_contents. I dont get errors as such… just seems like there is nothing retrurned. I have tested other XMLs, and they all load (although all are http rather than https). Is there something silly and simple I may have overlooked?
Thanks in advance
Mike
Nice job!
Here is an example of how you extract the data and then sort it by likes in PHP:
http://www.sundh.com/blog/2010/11/get-likes-on-urls-from-facebook/
[...] This site had the solution for Facebook likes. Let’s say your page, containing like and tweet buttons is at http://the.url.com. Simply type the following at the command line: curl “https://api.facebook.com/method/fql.query?query=select%20%20like_count%20from%20link_stat%20where%20url=%22http://the.url.com%22″ You’ll get back an XML document containing the like count. [...]
Hi Sascha,
I glad to get help from you. In case, i want to get list of users who have MY FACEBOOK PAGE, so could you please help me do that?
Thanks so much!
Hi,
This is really helpful and just what I was looking for. Thanks!!
I have one question though. If you use the url “http://www.facebook.com/radiohead”, you will get the correct numbers, but if the url is “http://www.facebook.com/microfiches”, all the numbers are shown 0, while a visit to that URL clearly shows that this band has 85 likes.
Can you please help me in understanding why is this happening?
Thanks
[...] How to get statistics for a Facebook Like link [...]
Sathish,
Try this query if you want the fan count of a page:
http://api.facebook.com/method/fql.query?query=select%20fan_count%20from%20page%20where%20page_id=
Simply add at the end your facebook page id:
http://api.facebook.com/method/fql.query?query=select%20fan_count%20from%20page%20where%20page_id=1111111111111111