PHP Get Likes Number Of Facebook Page
Posted by in Facebook Graph API November 16, 2011 5 Comments

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 "&lt;pre&gt;";
	var_dump($result);
	echo "&lt;/pre&gt;";
?>

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 "&lt;pre&gt;";
	var_dump($result);
	echo "&lt;/pre&gt;";
?>

+ 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.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • J

    Thank you. It’s really better and faster than using FQL or Facebook API.

  • http://www.yphpmysql.com/ PHP Development

    Hi there, just wanted to impart that I think this blog is brilliant, it has just the information I am looking for. Please feel happy about the fact that this is only the 6th blog I have bookmarked in over 6 weeks. Keep up the good work and I look forward to making some more important comments over the weeks.

  • josh

    Nothing related but I like to share something I’ve found VERY useful. I highly recommend everyone to use Get Likes Facebook http://getlikesfacebook.com/. It’s free and I get ton of likes everyday. TRUST ME! Try it and thank me later!

    • thankyou

      sound great! however, are they real people or robot???

  • Puppen

    My favorite site to get free Likes is GetLikesEasy. It’s a fast and easy Like Exchange system and I’ve already gotten more than a thousand Likes. Check it out! http://GetLikesEasy.com