2010-12-17 11 views
1

Ich benutze die offizielle PHP-Facebook-API und weiß, wie man die Freundesliste eines Benutzers bekommt. Es ist im Grunde wie folgt:PHP/Facebook-API: Update-Freundesliste

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

in meiner Anwendung im Grunde ich alle Freunde in meiner Datenbank speichern, so dass ich nicht eine http-Anfrage jedes Mal mache ich alle Freunde eines Nutzers angezeigt werden sollen. Aber jetzt möchte ich die Freundesliste aktualisieren. Ich würde es hassen, alle Freund-Verbindungen zu löschen und sie alle noch einmal zu speichern. Kennt also jemand eine Option, wie bekommt man die Änderungen der Freundesliste seit einem bestimmten Datum?

Antwort

2

Sie müssen dann überprüfen, ob sie in der Datenbank sind. Ich schlage vor, Sie erhalten ein Array aller Benutzer-IDs in der Datenbank und überprüfen sie dann gegen die Freunde, die Sie aus der API gezogen haben. Wenn es einen neuen Freund gibt, füge sie hinzu.

$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 
    } 
    } 
}