Sometimes you might wanna list things on your site order by the popularity on Facebook. It can also be a handy tool compare your different sites Facebook likes and get social stats from it. Facebook offer a very handy service to check this without being logged in to Facebook in any way. Check the service we use here. Facebook will retreive all Facebook likes, share count and total counts of these. For each link, it will display:
<url>http://www.google.com</url>
<share_count>295804</share_count>
<like_count>153002</like_count>
<comment_count>177545</comment_count>
<total_count>626351</total_count>
<click_count>265614</click_count>
<normalized_url>http://www.google.com/</normalized_url>
<comments_fbid>381702034999</comments_fbid>
</link_stat>
Get total likes on urls
With a loop in php we can get the information we need:
$urls = "";
// Add urls to check for likes
for($i = 0;$i < count($arr);$i++) {
if($urls != "") $urls .= ",";
$urls .= $arr[$i];
}
// Retreive info from Facebook
$facebookapi = "http://api.facebook.com/restserver.php?method=links.getStats&urls=" . $urls;
$xml = simplexml_load_file($facebookapi);
$likes = array();
// Loop through the result and populate an array with the likes
for ($i = 0;$i < count($arr);$i++) {
$url = $xml->link_stat[$i]->url;
$counts = (int)$xml->link_stat[$i]->like_count;
$likes[] = array("likes" => $counts, "url" => $url);
}
return $likes;
}
// Array with links to the URLs we want to get number of likes from
$array = array("http://www.google.com","http://www.apple.com","http://www.facebook.com");
$likes = getLikes($array);
foreach ($likes as $key => $val) {
echo $key . " => " . $val["url"] . " => " . $val["likes"] . "<br />";
}
Sorting the likes
After retrieving the likes, the array can be sorted by most popular url.
foreach ($likes as $key => $val) {
echo $key . " => " . $val["url"] . " => " . $val["likes"] . "<br />";
}














Sam P 23:37 on March 8, 2011 Permalink
Great script; however there are a couple of html transcription errors from the original code to how the site has displayed it:
(Hopefully my corrections display correctly in the comments and don’t suffer the same issue)
There are html entities < and > that need to be and couple of extra semi-colons that have been inserted.
Corrected lines:
5) for($i = 0;$i < count($arr);$i++) {
16) for ($i = 0;$i link_stat[$i]->url;
18) $counts = (int)$xml->link_stat[$i]->like_count;
30) $likes = getLikes($array);foreach ($likes as $key => $val) {
Sam P 23:39 on March 8, 2011 Permalink
Grrr it has removed my & gt ; and & lt ; automatically (hope it makes sense still)
Ellen 08:47 on March 9, 2011 Permalink
My WordPress seems to make weird things with the code. I corrected the code now!
Simas 14:48 on April 9, 2011 Permalink
Hi,
does this script show true? I’ve checked for a few websites but it returns a wrong information (i checked with facebook api url which you posted above)
Ellen 15:13 on April 9, 2011 Permalink
Hi Simas! Do you have an example of how it shows the wrong information? I know the the Facebook API shows unique shares but the Like-button can show more number of shares. You can increase the number of shares that the Like-button shows by likeing it and then removing the news on Facebook. Then you can like it again but the Like-button does not know that you are the same user likeing it.
Simas 13:41 on April 12, 2011 Permalink
Hi,
thanks, interesting info.
Check this out:
http://api.facebook.com/restserver.php?method=links.getStats&urls=http://it-darbas.lt/
At this time, the Like count equals to 289, but API service shows only 1. Why is that?
Simas 13:42 on April 12, 2011 Permalink
P.S. this is example isnt related to your script, but it gives bad results.
Ola 13:43 on April 18, 2011 Permalink
Am i missing something..it just get the numbers of like, what about the number of shares and comments?
Anyway the easiest way to get the likes for an object is just to do an API Graph Call. Since likes is a public data, you do not need an auth token
Try this – Type in your browser window
http://graph.facebook.com/?id=http://facebook.com
you should get something like this
{
“id”: “http://www.facebook.com”,
“shares”: 2534366,
“comments”: 121
}
you can then use json_decode to convert to an array and get your data.
See an example here
http://appshack.tv/#/2011/03/likes/
Johan 09:29 on May 5, 2011 Permalink
Can’t get the sorting to work. Is array_sort() the right function to use?
Ellen 09:51 on May 5, 2011 Permalink
@Johan The first code sorts it by the least popular at the top. If you want to sort by most popular at the top use a custom built function. In my case I use:
function array_sort($array, $on, $order=SORT_ASC)
{
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
Ellen 10:05 on May 5, 2011 Permalink
View source here: http://www.sundh.com/dev/get_likes.txt
@metacowboy 06:47 on May 14, 2011 Permalink
A really very nice tutorial , stumble the third time on it . Just one extension would be great how to list all Likes from a WP blog . Or even better woud it be possible to show how many times a user liked on Blog different posts ? would give a nice possibility to reward loyal visitors Thank you for the great work
Ellen 21:14 on May 14, 2011 Permalink
Thank you Metacowboy! There should be no problem making an extension displaying the “Like Toplist” of your WP blog. I do think it can be a problem getting info of how many times a user liked something on your blog since that is private to that user and her network on Facebook. It would be great though to be able to reward loyal visitors! Let me know if there is!
Roman 21:26 on May 20, 2011 Permalink
Cant understand why script is working sometimes, and sometimes not (showing zeros) while direct link with all urls is returning XML correctly…
Steve 19:36 on May 29, 2011 Permalink
@Simas
I looked at your site and facebook is not returning bad results. Your site has the wrong URL being liked. If you look at the code on your website it is linking to http://www.facebook.com/itdarbas instead of http://it-darbas.lt
http://api.facebook.com/restserver.php?method=links.getStats&urls=http://it-darbas.lt/
vs
http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.facebook.com/itdarbas
demetrius 17:40 on August 19, 2011 Permalink
Ellen, thank u for this awesome tutorial!
Ciprian 06:20 on September 8, 2011 Permalink
$urls = implode (”,”, $arr); //easier version of combine ourl
perusi 17:49 on February 21, 2012 Permalink
Hi Ellen, i`m a newbie in php, so my answer will seem stupid
…but, how do you make the array to be load from an .csv file?
Also, i have this picture link:
http://2.bp.blogspot.com/-SQJRif7G4vA/TgGyI-tP0RI/AAAAAAAAEKQ/KUgVxwZZXiI/s640/funny-pictures-of-animals-1.jpg?328
It haves 1 like and 1 share. But FB displays 0 to likes and 1 to share.
Same picture but with different end
http://2.bp.blogspot.com/-SQJRif7G4vA/TgGyI-tP0RI/AAAAAAAAEKQ/KUgVxwZZXiI/s640/funny-pictures-of-animals-1.jpg?128 —> this one have 2 likes, 2 share, but FB displays only 2 share and 0 likes
Do you have any idea why? Is it maybe because of the end? the ?328 and ?128 ???
thanks
get facebook like count | Abhijit Gaikwad 07:54 on June 10, 2012 Permalink
[...] Source, with PHP code for parsing the output: Get likes on urls from Facebook [...]
get facebook like count | TechInterviewPrep 00:00 on March 10, 2013 Permalink
[...] Source, with PHP code for parsing the output: Get likes on urls from Facebook [...]
marijana 13:17 on April 17, 2013 Permalink
i think its better to use the facebook graph api.
http://newexception.com/facebook-like-counter