【问题标题】:Facebook php sdk Retrieve friendlist issueFacebook php sdk 检索好友列表问题
【发布时间】:2011-04-22 22:57:52
【问题描述】:

我遇到了 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"]; 
?>

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



问题出在哪里?

【问题讨论】:

  • var_dump$frnd 开头。
  • 我编辑了 $frnd = $facebook ->api('/me/friends?access_token='.$accessToken); var_dump($frnd);相同的输出。 :(

标签: php list facebook sdk friend


【解决方案1】:
    //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']."' />";     
    }
    ?>

【讨论】:

    【解决方案2】:
        $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】:

      查询好友时无需添加access_token。 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'];
            }
         }
      

      【讨论】:

        【解决方案4】:

         是 BOM 表头: 见:http://en.wikipedia.org/wiki/Byte_order_mark (你应该将你的文件编码为 uff-8 没有 bom)

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

        【讨论】:

        • 是的,转换为 utf-8 w/o bom 解决了特殊问题。但是为什么我的代码不起作用??
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多