There are several ways to get the number of users who like a particular Facebook page, we can use both Facebook API or FQL to get that number. However, I’ll supply a simplest way to do that, just a few lines of code, we don’t need to register for a new Facebook app or request allow permission or anything.
In order to get the total number of likes, you just specify the Facebook Page Id or Name and use file_get_contents() PHP function to load return JSON encoded string from Facebook. In addition, the response string should be converted to an array by using json_decode() function for easier accessing.
PHP Get Likes Number Of Facebook Page
Assume that we need to get the likes number from 2 Facebook page, one with Facebook Name (4.Rapid.Development) and one with Facebook ID (19292868552).
+ Facebook Page – 4.Rapid.Development:
<?php $result = file_get_contents("https://graph.facebook.com/4.Rapid.Development"); $result = json_decode($result,true); echo "name: " . $result["name"] . "<br>"; echo "website: " . $result["website"] . "<br>"; echo "likes: " . $result["likes"] . "<br>"; echo "<pre>"; var_dump($result); echo "</pre>"; ?> |
Output:
name: 4 Rapid Development
website: http://4rapiddev.com
likes: 180
array(11) {
["id"]=>
string(15) "192737727433644"
["name"]=>
string(19) "4 Rapid Development"
["picture"]=>
string(80) "http://profile.ak.fbcdn.net/hprofile-ak-snc4/187791_192737727433644_418020_s.jpg"
["link"]=>
string(43) "http://www.facebook.com/4.Rapid.Development"
["likes"]=>
int(180)
["category"]=>
string(20) "Computers/technology"
["website"]=>
string(20) "http://4rapiddev.com"
["username"]=>
string(19) "4.Rapid.Development"
["founded"]=>
string(10) "Hoan Huynh"
["location"]=>
array(2) {
["city"]=>
string(16) "Ho Chi Minh City"
["country"]=>
string(7) "Vietnam"
}
["can_post"]=>
bool(true)
} |
+ Facebook Page ID 19292868552:
<?php $result = file_get_contents("https://graph.facebook.com/19292868552"); $result = json_decode($result,true); echo "name: " . $result["name"] . "<br>"; echo "website: " . $result["website"] . "<br>"; echo "likes: " . $result["likes"] . "<br>"; echo "<pre>"; var_dump($result); echo "</pre>"; ?> |
+ Output:
name: Facebook Platform
website: http://developers.facebook.com
likes: 3401015
array(11) {
["id"]=>
string(11) "19292868552"
["name"]=>
string(17) "Facebook Platform"
["picture"]=>
string(80) "http://profile.ak.fbcdn.net/hprofile-ak-ash2/276791_19292868552_1958181823_s.jpg"
["link"]=>
string(32) "http://www.facebook.com/platform"
["likes"]=>
int(3401015)
["category"]=>
string(15) "Product/service"
["website"]=>
string(30) "http://developers.facebook.com"
["username"]=>
string(8) "platform"
["founded"]=>
string(4) "2007"
["company_overview"]=>
string(78) "Facebook Platform enables anyone to build social apps on Facebook and the web."
["mission"]=>
string(37) "To make the web more open and social."
} |
As you can see in the 2 simple example above, beside the “likes” number, we can get some other information such as: name, website, link, picture, category, etc of a Facebook page easily as long as we have its Facebook ID or Name.
