开发者

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?


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


 is BOM header: See: http://en.wikipedia.org/wiki/Byte_order_mark (you should encode your file as uff-8 without bom)

This means your code isn't outputting anything.


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

    //print_r($friends['data']);
    print_r("Number of friends: ". count($friends['data']));

    foreach ($friends['data'] as $key=>$friendList) {
        echo "<br/>".$key." ".$friendList['name']."<img src='https://graph.facebook.com/".$friendList['id']."/picture' width='50' height='50' title='".$friendList['name']."' />";
    }


    //get user basic description using graph api
    $friends = $facebook->api('me?fields=friends');

    print_r("Number of friends: ". count($friends['friends']['data']));

    foreach ($friends['friends']['data'] as $key=>$friendList) {
        echo "<br/>".$key." ".$friendList['name']."<img src='https://graph.facebook.com/".$friendList['id']."/picture' width='50' height='50' title='".$friendList['name']."' />";     
    }
    ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜