2010-12-17 8 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要求を毎回作る持っていけないように、私は基本的に、私のデータベース内のすべての友人を保存します。しかし今、Friendlistを更新したいと思います。私はすべての友人接続を削除し、それらをもう一度保存することは嫌いです。だから誰もオプションについて知っている、特定の日付以来、友人リストの変更を取得する方法?

答えて

2

あなたがデータベースに入っているかどうかをチェックする必要があります。私はあなたがデータベース内のすべてのユーザーIDの配列を取得し、あなたが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 
    } 
    } 
}