Get FaceBook Friend Count

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.

Related posts

Leave a Reply

4 comments

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

  2. 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:

    <?php
    //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
                                'cookie'=>true,
                                ));
    
    $session = $facebook->getSession();
    $friends = $facebook->api('/me/friends');
    $friends_count=0;
    foreach($friends as $person){
     $friends_count = $friends_count + 1;
    }
    
    echo $friends_count;
    ?>
    

    enjoy!

  3. 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}
    
  4. There are lot of way to count the no of friends using Facebook Graph API with Php SDK, out of one i am telling..

    $fbuser=$fb->api('/me/friends');
    $access_token = $fb->getAccessToken();
    
    
    $friend_count=0;
    foreach($fbuser['data'] as $friends)
    {
      $friends_count++;
    }