Facebook php sdk检索好友列表问题

时间:2010-10-08 15:12:38

标签: php list facebook sdk friend

我有一个Facebook php sdk检索朋友列表问题。这是我的基本代码......

<?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"]; 
?>

但它会返回一个特殊的输出。



问题出在哪里?

4 个答案:

答案 0 :(得分:9)

当您查询好友时,您不必添加访问令牌。 Facebook-Api负责这一点。这是我的代码,对我有用:

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

答案 1 :(得分:2)

是物料清单标题: 请参阅:http://en.wikipedia.org/wiki/Byte_order_mark (你应该把你的文件编码为utf-8而不是bom)

这意味着您的代码没有输出任何内容。

答案 2 :(得分:1)

    $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']."' />";
    }

答案 3 :(得分:0)

    //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']."' />";     
    }
    ?>