Friday, January 28, 2011

How to Display Facebook Fan Count in Text

Amplify’d from www.hongkiat.com

How to Display Facebook Fan Count in Text

Adding a Facebook Like Button on your website or blog is perhaps one of the easiest and most effective way to get more fans and earn more likes. If you have hundreds, thousand or perhaps tens of thousand of fans, it might be a good idea to publish a fan count. Fan count displays in numeric text total fans your Facebook page has, or rather, how many have liked your page.

preview How to Display Facebook Fan Count in Text

The technique is simple and very similar to displaying Google feedcount in text. The only difference is – we will be using Facebook’s API. In this post, we want to show you how it can be done so you can add them into your WordPress blog or PHP powered website. Load up your favorite code editor, here we go.

Step 1 – Get API Key, Application Secret & Page ID

These are the three mandatory information you’ll need to get things started.

API Key and Application Secret

  1. Sign in to your Facebook and click on Developer‘s link on the site bar. (If you don’t have a Developer’s account, you will have to sign up for one first)

    facebook developer link How to Display Facebook Fan Count in Text


  1. Click "Set Up New Application". Enter any application name, agree to the Facebook terms and click "Create Application". Note: We are not going to create a real application, but we’ll need to go through these processes to get a API Key and Application Secret.

    create application How to Display Facebook Fan Count in Text
  1. On the next page, you should be able to see your personal API Key and Application Secret. Copy it down somewhere or leave the browser window open.

    api key How to Display Facebook Fan Count in Text

Page ID

Page ID is the ID of your Facebook Fan Page. If you don’t have a Facebook Fan page, you’ll have to first create one. To get the ID of your Fan page, click edit page, and look for your ID (numbers) at the end of the URL – http://www.facebook.com/pages/edit/?id=XXXXXXXXX

Step 2 – Get Facebook.php

You’ll also need the Facebook API PHP SDK file called Facebook.php. Download it, and place it in the root of your folder. Learn more about Facebook PHP SDK.

php sdk How to Display Facebook Fan Count in Text

Step 3 – Display Fancount

To show off your fancount, place the following code in your php file,

  1. <?php  
  2. require_once('facebook.php');  
  3. $facebook = new Facebook(array(  
  4.     'appId'  => 'app_id',  
  5.     'secret' => 'secret_key',  
  6.     'cookie' => true,  
  7. ));  
  8. $result = $facebook->api(array(  
  9.     'method' => 'fql.query',  
  10.     'query' => 'select fan_count from page where page_id = page_id;'  
  11. ));  
  12. $fb_fans = $result[0]['fan_count'];  
  13. ?>  

And then you will want to edit the codes to change the following:

  • Line 4 – Replace app_id with your Facebook Application ID.
  • Line 5 – Replace secret_key with Secret.
  • Line 10 – Replace the last page_id with your Facebook Fanpage ID.

Please refer to Step 1 on how to get app_id, secret_key and page_id.

To display the fancount, insert this following code anywhere in the page:

  1. <?php echo $fb_fans; ?>  

That’s all. Style your fancount and make it attractive. See a working example on Psdeluxe‘s top right corner.

Author: Dainis

Dainis is blogger from Latvia. He is webmaster of Psdeluxe.com (Photoshop deluxe) - Design community where you find photoshop tutorials and useful resources for web and graphic designers. Follow Dainis on Twitter

Read more at www.hongkiat.com
 

1 comment:

  1. This is a very helpful post, I hope this really helps me to complete my project.

    custom fan page

    ReplyDelete