Is there a simple PHP or Javascript method of getting the total number of FaceBook friends? Twitter makes it super easy to do that, and I need to do the same for one of my WordPress installs.

4 Answers 4


To get your friends as a JSON object from Facebook, you can use their Graph API. Easiest way is to visit this page: http://developers.facebook.com/docs/reference/api/user/

Scroll down until you find the Friends link in the "Connections" table. That link will give you a JSON object containing all your friends. Note that the URL on that page is unique to you and contains your access token. Don't share the link.

You can use this sort of code in WP to get and use that information:

$body = wp_remote_retrieve_body(wp_remote_get('YOUR_FB_URL', array('sslverify'=>false)));
$dec = json_decode($body);
echo count($dec->data);

You'll want to use transients or something similar to cache the data so that you don't ask FB for it all the time.

  • In your code above, is the YOUR_FB_URL the url to the JSON object?
    – Pippin
    Jan 19, 2011 at 16:27
  • I haven't been able to get this to work? Any more feedback?
    – Pippin
    Jan 21, 2011 at 3:07
  • 1
    this works fine, YOUR_FB_URL is the link you get from developers.facebook.com/docs/reference/api/user under connections table >> Friends . make sure you are logged in to facebook and you have the developers app in your profile.
    – Bainternet
    Feb 2, 2011 at 10:14
  • Yes, the YOUR_FB_URL would be replaced with the URL of the JSON object I pointed you to earlier. I tested this code, it does work.
    – Otto
    Feb 3, 2011 at 7:31
  • Thanks Otto, I trust you and will test for sure in a bit.
    – Pippin
    Feb 7, 2011 at 20:39

yes facebook does have a way for you to do that. you can use the facebook graph api and its user|friend connection

something like this:

first create an application at http://www.facebook.com/developers/ you might need to add the developer application to your profile and you will see a button "Set Up New App".

then download this facebook php wrapper from here https://github.com/facebook/php-sdk/blob/master/src/facebook.php

and then you can work with this PHP code:

//require the file you downloaded
require 'facebook.php';

$facebook = new Facebook(array(
                            'appId' =>'myid', //change to your APP ID
                            'secret'=>'mysecret',  //change to your SECRET code

$session = $facebook->getSession();
$friends = $facebook->api('/me/friends');
foreach($friends as $person){
 $friends_count = $friends_count + 1;

echo $friends_count;


  • Hmm, that's not working. Do I need to change the /me/friends to anything?
    – Pippin
    Jan 19, 2011 at 19:29
  • 1
    yes, and the appId and secret in the array up there too
    – kaiser
    Feb 4, 2011 at 1:18

You can get friend count by fql easily.

There is a friend_count field against the user table that you can query.

SELECT friend_count FROM user WHERE uid = me();

https://graph.facebook.com/fql?q=SELECT friend_count FROM user WHERE uid={any id}

There are lot of way to count the no of friends using Facebook Graph API with Php SDK, out of one i am telling..

$access_token = $fb->getAccessToken();

foreach($fbuser['data'] as $friends)

Not the answer you're looking for? Browse other questions tagged or ask your own question.