Nitin Chandak
Nitin Chandak

Reputation: 11

How to display the list of user's friends in Facebook php?

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.

Upvotes: 1

Views: 3049

Answers (2)

Somnath Muluk
Somnath Muluk

Reputation: 57766

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'];
     }

Upvotes: 2

Manu Manjunath
Manu Manjunath

Reputation: 6421

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";
}

Upvotes: 1

Related Questions