PHP / Facebook-Api: Update Friendlist
I'm using the official PHP-Facebook-Api and I know how to get the friendlist of a user. It is basically like so:
$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 F开发者_如何学Criends 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 my Application I basically save all Friends in my database, so that I dont have make an http-request everytime I want to show all Friends of a user. But now I would like to update the Friendlist. I would hate to delete all friend-Connections and save them all over again. So does anybody know about an option, how to just get the changes of the friendlist since a certain date?
Your going to have to check if they are in the database then. I suggest you get an array of all the user id's in the database and then check them against the friends you pulled from the API. If there is a new friend, add them.
$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
}
}
}
精彩评论