Advertisements

How to get your Facebook follower count in PHP

If you’re looking for a way to fetch and display your Facebook follower count in PHP, here is your code. In the below, substitute __ID__ for your numeric ID for your app or page, and __TOKEN__ for your access token.

Please consult the Facebook Graph API documentation for more information.

This method uses file_get_contents() rather than the preferrable cURL() call. Also, don’t forget to cache your queries, or you may experience API throttling. See the Rest & Graph API Best Practices for reference on this.

Here is the code:

function GetFacebookFollowerCount(){
  $json = file_get_contents('https://graph.facebook.com/__ID__/insights/page_fans/lifetime?access_token=__TOKEN__');
  $obj = json_decode($json);
  $new_facebook_followers= $obj->data[0]->values[0]->value;
  return $new_facebook_followers;
}

Comments and feedback are welcome.

Advertisements

, ,

  1. #1 by MegB on March 25, 2011 - 6:40 am

    Ohhk… now I get this thanks a lot for the code. Let me try by own.