2010-12-17 106 views
1

我使用官方PHP-Facebook-Api,我知道如何獲取用戶的好友列表。它基本上是像這樣:PHP/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']; 
     } 
    } 

在我的應用我基本上保存所有的朋友在我的數據庫,讓我沒有做一個http請求,每次我想顯示用戶的所有好友。但是現在我想更新好友列表。我不願意刪除所有的朋友連接,並再次保存它們。那麼是否有人知道一個選項,如何從某個日期開始獲取好友列表的更改?

回答

2

你將不得不檢查他們是否在數據庫中。我建議你在數據庫中得到所有用戶標識的數組,然後檢查你從API中提取的朋友。如果有新朋友,請添加它們。

$database = new mysqli('Localhost', 'username', 'password', 'db_name'); 

if($users = $database->query('SELECT id FROM `users`')) { 
    while($row = $result->fetch_assoc()) { 
     $my_friend[] = $row['id']; 
    } 
} 

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

    if(!in_array($id, $my_friends)) { 
     // Insert into table 
    } 
    } 
}