How to retrieve facebook user’s friendlist


facebookSometimes you may need to retrieve user’s friend-list in facebook. You can retrieve user friends list in several ways. Using new graph api you can retrieve friend-list or you can call legacy api to retrieve friend-list.

Using graph api:

function getUserFriendList($facebook){
        $friends     =   $facebook->api('/me/friends');
        return $friends;
}

Using legacy api:

function getUserFriendList($facebook, $uid){
    try {
        $param  =   array(
                'method'  => 'friends.get',
                'uid'     => $uid,
                'flid'    => '',
                'callback'=> ''
        );
        $friends   =   $facebook->api($param);
        return $friends;
    }
    catch(Exception $o) {
        d($o);
    }
    return '';
}
mahmud ahsan

Love computer programming and write occasional blog post. In my early career I worked mostly on web based application and since 2011, I exclusively working on iPhone and android applications. I work at home, father of a nice kid and husband of a beautiful wife. [My Business | Twitter | Linkedin]

You may also like

66 Comments

  • Alexander
    July 14, 2010 at 2:33 am

    Can I get friendlist of any user using this method? Or only friendlist of user that using my app?

    • mahmud ahsan
      mahmud ahsan
      July 14, 2010 at 12:14 pm

      No you can’t retrieve any user’s friendlist. You can retrieve only app user’s friendlist when the user logged in your facebook application.

      • Alexander
        July 14, 2010 at 3:10 pm

        Thank you for reply. So I can’t make friends tree (show my friends and friends of my friends in tree-view)?

        • Avedis
          November 23, 2010 at 7:45 pm

          I guess you also have to include it in the application permissions… access_token is not enough!

      • arabinda
        October 1, 2011 at 4:30 am

        i am getting error for retryping freinds name from facebook through the api bellow are is my code

        ‘276868289004400’,
        ‘secret’ => ‘6de4440db71ab726bb25c7b3ab3da257’,
        ));

        $friends = $facebook->api(‘/me/friends’);
        return $friends;

        ?>

        bellow the result which comes out

        Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /home/itinfopa/public_html/fbaccount/facebook-php-sdk-v2.1.2-0-gb14edfa/facebook-php-sdk-b14edfa/src/facebook.php on line 543

        • mahmud ahsan
          mahmud ahsan
          October 2, 2011 at 3:57 pm

          use sdk version 3.0

      • riccardo
        November 21, 2011 at 4:38 pm

        ok, my user is logged into app, but if i use $facebook->api(‘/me/friends) i contune to get all friends , not just those who use it. How is possible?

      • Prasanna Kumar
        May 7, 2012 at 10:47 am

        what if my friend has made the friend list public !! then is there any way to see that public list or even if its been private we can see the mutual frnds so at any case is there any solution to see the friends list or mutual frnds using a facebook app and if so please give the code to do please !!

    • Johns
      January 1, 2013 at 5:37 pm

      How can i get friends list through facebook SDK?

  • William
    July 18, 2010 at 10:02 am

    How i can retrieve logged in user friend list using my app?
    this method retrive all user friend list right?

  • ccarrasco
    July 20, 2010 at 8:58 pm

    which of these methods is better? Legacy API or Graph Api?

    • mahmud ahsan
      mahmud ahsan
      July 20, 2010 at 10:01 pm

      i think graph api is better as it retrieves friends name also.

  • fahad mullaji
    July 21, 2010 at 3:16 pm

    dude..i dont no dis language.. i want process but in asp.net..pls help..:):):)

  • Justn Cribb
    July 22, 2010 at 9:40 am

    How can I retrieve user’s friends that is also using the app in Graph API? Uugh, the Facebook Graph API docs is horribly documented. Right now, I’m using friend pile using the new Javascript SDK…I want to know how to do this using graph api? Thank you ^^

    • mahmud ahsan
      mahmud ahsan
      July 22, 2010 at 11:09 am

      This is graph api calling using js-sdk.

      FB.api('/me/friends', function(response) {
      	      //friends
      });
      
      • akaayy
        July 31, 2010 at 5:02 pm

        can you tell me how to retrieve user’s friends that is also using the app Quickly.

  • akaayy
    July 30, 2010 at 9:23 am

    I retrieve whole friendlist of any user using this method, then Contrast my db to check if everyone is my app user? This seems inefficient.
    There are better ways to do?

  • Marco
    August 26, 2010 at 4:15 am

    Receiving the user’s friendslist works fine in session context with /me/friends.

    Is there a way to receive this friendslist via a cronjob, when the user is NOT logged in. It should be possible as the user allowed me to access his friends.

  • Manish
    August 26, 2010 at 9:18 pm

    Hi mahmud ahsan,
    Sir do you have any idea how to retrieve “facebook freinds feeds” while i have user offline permission.

    Please let me know thanks.

  • bailey
    September 11, 2010 at 12:36 pm

    Hi,
    Would you know how to get the actual username of the user with this:

    $facebook->api(array(‘method’ => ‘friends.get’,’name’ => $uid));

    Thanks.

  • Furqan
    September 20, 2010 at 12:13 pm

    Salam , I have seen posts on my wall from applications , but with my friends name . i want to do the same , that my application gets the friends of the current user and posts to walls of all of them . how is that possible , can you please direct me to the right direction.

    Thanks,
    Furqan

    • mahmud ahsan
      mahmud ahsan
      September 21, 2010 at 3:05 am

      Please check out my other tutorials.

  • Chris
    September 27, 2010 at 2:38 pm

    hi mahmud,
    great tutorials, i love them!
    is there any way to get the list of fans of my app / the people who like or use my application?
    kind regards and thanks,
    chris

  • Casey
    October 20, 2010 at 4:26 pm

    Dear Mahmud,
    thanks for the great tutorials.
    I successfully retrieve friend list by graph api.

    $friends = $facebook->api(‘/me/friends’);

    However, there are only two attributes (name& id) in the JASON file.
    Would u know is there any way to get their photo thumbanail ?

    Thanks so much & best regards,
    Casey

    • mahmud ahsan
      mahmud ahsan
      October 21, 2010 at 12:43 am

      so you could easily get user photo from their uid.

      <img  src="https://graph.facebook.com/<?=$facebook_user_id?>/picture" />
      
      • Casey
        October 21, 2010 at 2:06 pm

        Thanks mahmud, that’s a great help !

        There are still some problem using graph api to post messages on friends’ wall.
        I’ve tried add
        ‘req_perms’=>’publish_stream’,
        into facebook.php (line428)

        But, I still cannot post messages on my friends’ walls by
        $statusUpate = $facebook->api(‘friend_id/feed’,’post’,array ……);

        Is there any solution ?

        Thanks again and best regards,
        Casey

  • Casey
    October 21, 2010 at 2:37 pm

    Hey, answer to myself.
    Both ‘me’ and the user of ‘friend_id” should authorized the application.
    ^^

  • Javier
    October 26, 2010 at 9:04 am

    Hi, I’m using JS SDK… i’ve seen you put how to retrieve friend list with this JS… i’m just a beginner with all this facebook’s stuff, so coul you help me wtih this> i want to retrieve a list with friends’ photos and current location so i can use than info… please help me.
    Thanks

    • mahmud ahsan
      mahmud ahsan
      October 26, 2010 at 11:20 am

      Hello Javier, please checkout my other tutorials in popular section. Then you’ll learn how to setup and develop facebook application.

  • Yousouf
    November 3, 2010 at 2:03 am

    Mahmud
    your code will give me user’s friends’ names,ids, but I need the birthday of friends. I am using PHP sdk. Could you post a piece of code showing how to ask for the permissions and getting friends’ birthday. Thanks.

  • rajesh
    November 3, 2010 at 3:59 pm

    Is there a way to receive this friendslist via a cronjob, when the user is NOT logged in. It should be possible as the user allowed me to access his friends.

  • saira
    November 5, 2010 at 1:07 am

    i want to access user frinds lish,,i have use the method friends.getLists but empty list is shown,having no error,,,
    can u guild me whats wrong with this mentog,,thanks

  • quang
    November 24, 2010 at 3:22 pm

    After getting friends list, how to show name of friend?

    I use foreach but it’s not ok:

    foreach($friends as $friend){
    echo $friend[‘name’];
    }

  • garyok
    January 18, 2011 at 9:12 am

    Hey dude! nice post, i have a doubt,

    i want to send a message to a selected friends of my list, i want to do this as private message un fb or send an email to him , or simply notify him that he was added to my app,

    this can be done in some way?

    Thanks!

  • Manish P
    February 18, 2011 at 12:05 pm

    Hi mahmud ahsan,
    hope you are doing good.
    $_REQUEST[“fb_sig_user”]; is not working for fbml tab based application.
    what is the alternative for the same.
    I just want to capture the facebook userid who is playing my application.
    Please do the needful.
    Thanks in advance.

  • Ram
    February 20, 2011 at 11:04 am

    What will be the best way to store friends list in the database?

  • ash
    February 22, 2011 at 8:00 pm

    Hello mahmud! Thanks for your tutorials! They are of great help…:)
    anyway do you know how to check if two users are already connected on facebook, that is are friends?
    Is there a way to do this using graph api?
    Thank you

  • linjo
    March 5, 2011 at 8:47 am

    Hi mahmud,

    I tried this code,and its not working for me.Pls let me know whether this is because of the change in facebook api.If so whats the new code? Pls help me I have been trying for Days

  • Bibhash
    March 23, 2011 at 5:29 pm

    Hi Mahmud,

    I am trying to retrieve profile information like education and work history for people in my friend list. How can I do that? Do I require permission from each one of them??

    Thanks in advance!

    • xainee khan
      October 28, 2011 at 9:37 pm

      $user_profile = $facebook->api(‘/me’);
      this will return an array and part of this is like
      [education] => Array
      (
      [0] => Array
      (
      [school] => Array
      (
      [id] => 110822202275004
      [name] => sps college swat
      )

      [year] => Array
      (
      [id] => 113125125403208
      [name] => 2004
      )

      [type] => High School
      )

      [1] => Array
      (
      [school] => Array
      (
      [id] => 109989835697638
      [name] => NWFP University of Engineering & Technology
      )
      now use this $school_name = $userInfo[‘school’][‘name’];
      $employer_name = $userInfo[’employer’][‘name’];

      print the arrar through print_r(); u ll know what u r retriving. do the same thing like
      $school_name = $userInfo[‘school’][‘name’];
      $employer_name = $userInfo[’employer’][‘name’];

  • John McGovern
    April 13, 2011 at 7:34 am

    Thank you for helping us with our facebook programming!

  • Abhay
    April 27, 2011 at 6:49 pm

    How can i get profile information like education and work history for people in my friend list ?

    I am already asking for permissions of friend work history and education history.

    Please Help .

  • lili edryana
    May 2, 2011 at 12:29 am

    can you show some code which list fb friends in dropdown list.?

  • nanda
    May 10, 2011 at 5:50 pm

    how to get friends birthdays list,
    Thank you for ur tutorial

  • rushi
    June 6, 2011 at 3:08 pm

    i tried to get facebook userdata but i got error…
    Exception Object
    (
    [message:protected] => Facebook needs the JSON PHP extension.
    [string:private] =>
    => 0
    [file:protected] => /projects/p41/www/html/interworld.co.in/RechargeNation/facebook_rd/facebook.php
    [line:protected] => 6
    [trace:private] => Array
    (
    [0] => Array
    (
    [file] => /projects/p41/www/html/interworld.co.in/RechargeNation/facebook_rd/fbmain.php
    [line] => 7
    [function] => include_once
    )

    )

    )
    i dont have install json…..
    can i use xml instead of json….plz explain the way where i can use xml …in place of json_decode…..so that i need not to install json

    plz help

  • ask123
    June 10, 2011 at 9:38 pm

    what is the $facebook in the code means?? is it the userid of person from whom we are going to get the friend list……..

  • sanjay
    June 24, 2011 at 2:42 pm

    is it possible to acess facebook user friends email ?

  • Todd
    October 18, 2011 at 10:25 am

    What is the $facebook variable? I’m trying to use the legacy api and the first variable in the function is $facebook, but I don’t know what to put in for that variable when calling the function. Also, is the $uid variable the user id?

    How would I use this function? Would I use a “foreach” loop ?

    Thank you for your help!

  • xainee khan
    October 28, 2011 at 8:42 pm

    i want to retrive facebook user friend list (close_friends, acquaintances, restricted,user_created, education, work, current_city or family) .i guess this would not work in my scene.i have this permission (ead_friendlists,manage_friendlists).how to do this through graph api .please reply

  • Ravi S.Gohil
    January 6, 2012 at 12:25 pm

    How do I display the list of friends after retrieving the names and id.

  • Ravi S.Gohil
    January 6, 2012 at 3:52 pm

    This method $facebook->api(‘/me/friends’);
    retrieves only freidns using the common application as me.
    I want to retrieve the list of all my friends.

  • swati
    January 20, 2012 at 7:06 pm

    string query = “SELECT name,online_presence,status FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 =” + mid + “)”;
    string xmrrd = api.Fql.Query(query);
    DataSet ds = new DataSet(query);
    ds.ReadXml(new StringReader(xmrrd), XmlReadMode.InferSchema);
    GridView1.DataSource = ds.Tables[0].DefaultView;

    GridView1.DataBind();
    Hi,I’m using this code in asp.net but datagridview displays only true.
    Please tell me how what is going wrong in this code?

  • Mike
    February 23, 2012 at 10:00 pm

    Hi, why would I need a users friends list? What can I do with it?

  • Pooja
    April 17, 2012 at 4:18 pm

    hiii…………. how to call that Javascript function(above code) means what is that parameter $Facebook how to pass it??

  • Ravi Gupta
    May 22, 2012 at 4:59 pm

    Hi Ahsan, Does this provides friend’s emails as well?

    Thanks,
    Ravi

  • Wallace Hermano
    June 15, 2012 at 8:54 pm

    I’m using this code:

    $friends = $facebook->api(‘me/friends’);
    //print_r($friends[‘data’]);
    print_r(“Number of Friends: “. count($friends[‘data’]));
    foreach ($friends[‘data’] as $key=>$listOfFriends) {
    echo “”.$key.” “.$listOfFriends[‘name’].””;
    }

  • Shashank
    June 21, 2012 at 3:36 pm

    Hey,
    I have access to the friendlist. But now i want to save this friendlist(name and id) into a database.
    Can anyone please help me how should i go about it.

  • Juanjo
    September 23, 2012 at 8:19 pm

    Good post.

    But I get this error using graph lib ray

    Unsupported operation

    any idea?

  • Nilesh Gupta
    October 31, 2012 at 11:50 am

    I wanted to download the source file of the up code. Please provide me source file for fetch the facebook friends.

  • Mahender
    February 16, 2013 at 11:41 pm

    how to get geo location using api…

Comments are closed here.

About Me

Hi I am Mahmud Ahsan. I Love computer programming and write occasional blog post. In my early career I worked mostly on web based application and since 2011, I exclusively working on iPhone and android applications. I work at home, father of a nice kid and husband of a beautiful wife. [My Business | Twitter | Linkedin]

Twitter Feed

Fanpage