Facebook is currently one of the largest websites on the internet. It is a wise decision to have a Facebook page for your website for a number of reasons. In this tutorial, I am going to show you how to get the total number your Facebook fans (or people who like your page). There are many ways to get the total number of your fans. However, I will show you how you can do it using Facebook PHP SDK which means we are setting up an application, and then we use the Facebook API to call for our fans number. 1. Create a Facebook page. If you have a page already, go to step 2. To create a Facebook page, login to your account and follow this link. Choose “Brand or Page”. 2. Create a Facebook Application If you do not have an application, learn how to create a Facebook application. You are requested to have an application on Facebook in order to have a APP ID + APP SECRET. After you get your APP ID + APP SECRET, you are ready to go to the next step. 3. Download Facebook PHP SDK From Github This is the PHP SDK of Facebook, after you download the file. Upload it to your server because you will have to include it in your code later on. Download from here. 4. The Code! Now that you have got your Facebook and your application, open your favorite text-editing program, such as Aptana and create a page and call it: fb_fans.php
require_once("facebook.php"); // this is the file that you downloaded in step 3.
$config = array();
$config['appId'] = 'XXXXXXXX'; // insert your APP ID you got from step 2.
$config['secret'] = 'XXXXXXXX'; // insert your APP SECRET you got from step 2.
$config['fileUpload'] = false; // this is optional, you can leave it or delete it.
$facebook = new Facebook($config); // you are creating an object calling it $facebook.
$fql = "SELECT fan_count FROM page WHERE username = 'XXXXX'"; // XXXXX is your username.
$response = $facebook->api(array(
'method' => 'fql.query',
'query' =>$fql,
));
$response_count = $response['0']['fan_count']; // that's it! use $response_count to show the total likes.
echo "Total Fans: " . $response_count;
The output of this code will be something like: Total Fans: 351