Search code examples
phplistfacebooksdkfriend

Facebook php sdk Retrieve friendlist issue


I got a Facebook php sdk Retrieve friendlist problem. Here is my basic code...

<?php 

require_once 'fb-sdk/src/facebook.php';

// Create our Application instance.
$facebook = new Facebook(array(
  'appId'  => 'xxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxx',
  'cookie' => true,
));


$accessToken = $facebook->getAccessToken();
$session = $facebook->getSession();
$uid = $facebook->getUser(); 
 //echo "https://graph.facebook.com/".$uid."/friends/?access_token=".$accessToken;

$frnd = $facebook ->api('/me/friends?access_token='.$accessToken);
echo $frnd["data"][0]["name"]; 
?>

But it returns a peculiar output.



Where is the problem?


Solution

  • You dont have to add the access_token, when you query for the friends. The Facebook-Api takes care of that. This is my code, which works for me:

        $facebook = new Facebook(array(
                    'appId' => 'xxxxxxxx',
                    'secret' => 'xxxxxxx',
                    'cookie' => true,
                ));
        // $session is only != null, when you have the session-cookie, that is set by facebook, after the user logs in
        $session = $facebook->getSession(); 
        // you dont get a list of friends, but a list, which contains other friendlists
        $friendsLists = $facebook->api('/me/friends');
    
        // Save all Friends and FriendConnections
        foreach ($friendsLists as $friends) {
          foreach ($friends as $friend) {
             // do something with the friend, but you only have id and name
             $id = $friend['id'];
             $name = $friend['name'];
          }
       }