1

I am trying to display the user's friends by using the following code :

 $friends=$facebook->api('/me/friends');
 foreach($friends as $key=>$value)
 {
   foreach($value as $fkey=>$fvalue)
    {
     echo $fvalue->name;
    }
 }

But I do not get the user's friends name. I am new to Facebook app development so please help me out with this. Also how can I show the user's friends using the Graph API.

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Nitin Chandak
  • 11
  • 1
  • 2

2 Answers2

2

This is data format given by $facebook->api('/me/friends').

Array
(
    [data] => Array
        (   [0] => Array
                (
                    [name] => abc
                    [id] => 503099471
                )    
            [1] => Array
                (
                    [name] => dff
                    [id] => 565687398
                )
     )
    [paging] => Array
        (
            [next] => https://graph.facebook.com/me/friends?method=GET&access_token=AAAEYSMWjFY4BAG4QCxRU44BTcBwMajX4IrAVqhYzObAGVsDyuyhAsFf9MamBRaFD2eQhZBRwty3YgTbPbiAzfFlftPDjiTGZC06t5VQgZDZD&limit=5000&offset=5000&__after_id=100003479505340
        )
)

Use this code to display list of user's friends in facebook php:

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

     foreach($friends['data'] as $key=>$value)
     {
            echo $value['name'];
     }
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
1

You API call is fine. But the return value is structured differently than you think. Facebook returns an array of IDs and names for you call. So it can be accessed as follows:

$ret=$facebook->api('/me/friends');
$friends=$ret['data'];
for($i=0;$i<count($friends);$i++) {
      $friend=$friends[$i];
  echo "{$friend['id']}: {$friend['name']}\n";
}
Manu Manjunath
  • 6,201
  • 3
  • 32
  • 31
  • Facebook has a limit on number of friends that it returns for the **$facebook->api('/me/friends')** Graph API call. If you want to get next set of friends, you've to make **another graph api call** using the URL provided in **$ret['paging']['next']** in above code. – Manu Manjunath Mar 27 '12 at 05:23