25

我正在使用the most recent version of the Facebook SDK(雖然我不確定,但它可以連接到稱爲'圖形API'的東西)。我已經調整了Facebook的示例代碼,讓我可以連接到Facebook,並且工作正常......但我無法獲得我的朋友列表。使用最新的API獲取Facebook朋友列表

$friends = $facebook->api('friends.get'); 

這將產生錯誤消息:「致命錯誤:未捕獲OAuthException:(#803),有些要求你不存在的別名:friends.get拋出/mycode/facebook.php在線543」

不知道爲什麼這是或意味着什麼。有人能告訴我正確的語法(用於最新的Facebook API)來獲取朋友列表嗎? (我試過「$ friends = $ facebook-> api-> friends_get();」並得到不同的錯誤,「致命錯誤:在/mycode/example.php中的非對象上調用成員函數friends_get()行129「)。

我可以確認,在我的代碼中的這一點之前,事情都很好:我連接到Facebook與一個有效的會議,我可以得到我的信息並將其轉儲到屏幕上...即該代碼完全執行失敗friends.get呼叫前:

$session = $facebook->getSession(); 
if ($session) { 
    $uid = $facebook->getUser(); 
    $me = $facebook->api('/me'); 
} 
print_r($me); 

回答

59

我想這是你想要什麼:

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

由喲知道如何讓別人的個人資料圖片與API調用的任何機會呢?例如... $ friends = $ facebook-> api('/ idofosmefriend/picture');還有一些所有這些電話的文件?,謝謝! – 2010-10-08 22:13:38

+0

@nfvs,你知道我可以如何認識friends_hometown嗎? – alex 2011-07-15 20:39:59

+13

這不再適用於Graph API v2或甚至傳統的Graph API v1應用程序。請參閱:https://developers.facebook.com/docs/graph-api/reference/v2.0/user/friends 「這隻會返回誰使用(通過Facebook登錄)的應用程序提出請求的任何朋友。」 – goodies4uall 2014-05-01 14:03:32

13

戈像@nfvs描述的朋友是一個好方法。它與屬性id和名稱(按ID排序)的所有朋友一起輸出一個多維數組。你可以看到這樣的朋友照片:

foreach ($friends as $key=>$value) { 
    echo count($value) . ' Friends'; 
    echo '<hr />'; 
    echo '<ul id="friends">'; 
    foreach ($value as $fkey=>$fvalue) { 
     echo '<li><img src="https://graph.facebook.com/' . $fvalue->id . '/picture" title="' . $fvalue->name . '"/></li>'; 
    } 
    echo '</ul>'; 
} 
+0

工作!謝謝,只有一行爲我拋出一個錯誤....在第二個foreach循環中...我將其更改爲以下所以它爲我工作,回聲'

  • Stevanicus 2011-05-10 22:15:17

  • +0

    它的工作,按您的建議,並同在代碼更新的感謝Stevanicus&Geoffa – Elankeeran 2011-11-03 19:35:32

    -3

    header('Content-type:text/html; charset = utf-8');

    輸入您的網頁。

    3

    如果你想使用REST終點,

    $friends = $facebook->api(array('method' => 'friends.get')); 
    

    否則,如果您使用的是圖形API,然後使用,

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

    使用FQL相反,它是一個像SQL,但對於Facebook的數據表,並輕鬆涵蓋你想要的數據查詢。 你不需要使用所有這些/ xx/xxx/xx調用,只需知道你被關聯的表和列。

    $myQuery = "SELECT uid2 FROM friend WHERE uid1=me()"; 
    $facebook->api("/fql?q=" . urlencode($myQuery)) 
    

    大互動的例子在http://developers.facebook.com/docs/reference/fql/

    24

    這是PHP代碼的真人版,以從Facebook

    得到您的朋友
    <?php 
        $user = $facebook->getUser(); 
    
    
        if ($user) { 
         $user_profile = $facebook->api('/me'); 
         $friends = $facebook->api('/me/friends'); 
    
         echo '<ul>'; 
         foreach ($friends["data"] as $value) { 
          echo '<li>'; 
          echo '<div class="pic">'; 
          echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>'; 
          echo '</div>'; 
          echo '<div class="picName">'.$value["name"].'</div>'; 
          echo '</li>'; 
         } 
         echo '</ul>'; 
        } 
    ?> 
    
    +0

    的文件說,它顯示誰也使用你的應用程序中的朋友,我上面的代碼已經嘗試並得到了這一結果。 。看看他們的鏈接:(檢查read_friendlists部分)https://developers.facebook.com/docs/facebook-login/permissions/v2.2 – 2015-01-06 09:04:16

    0

    Using CodeIgniter OAuth2/0.4.0 sparks,

    in Auth.php file,

    $user = $provider->get_user_info($token); 
    
    $friends = $provider->get_friends_list($token); 
    print_r($friends); 
    

    and in Facebook.php file under Provider, add the following function,

    public function get_friends_list(OAuth2_Token_Access $token) 
        { 
          $url = 'https://graph.facebook.com/me/friends?'.http_build_query(array(
            'access_token' => $token->access_token, 
          )); 
          $friends = json_decode(file_get_contents($url),TRUE); 
    
          return $friends; 
        } 
    
    prints the facebenter code hereook friends. 
    
    0
    friends.get 
    

    是函數獲取的朋友的

    列表是的,這是最好的

    $friends = $facebook->api('/me/friends'); 
    
        echo '<ul>'; 
        foreach ($friends["data"] as $value) { 
         echo '<li>'; 
         echo '<div class="pic">'; 
         echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>'; 
         echo '</div>'; 
         echo '<div class="picName">'.$value["name"].'</div>'; 
         echo '</li>'; 
        } 
        echo '</ul>'; 
    
    7

    剛擡起頭對任何人使用圖形API的2.0版過這個問題絆腳石:這將不工作了。在V2.0中,調用/我的/朋友只返回此人的朋友誰也使用應用程序列表:在最新版本的Facebook SDK的

    • A user access token with user_friends permission is required to view the current person's friends.
    • This will only return any friends who have used (via Facebook Login) the app making the request.
    • If a friend of the person declines the user_friends permission, that friend will not show up in the friend list for this person.
    +2

    有沒有什麼辦法可以得到所有朋友與2.0圖形API的列表? – Eric 2014-05-30 15:38:51

    3

    ,Facebook已經關閉,讓你訪問某些功能一個好友列表由於安全原因...檢查,以瞭解更多的文檔......