2010-12-17 6 views
1

나는 공식 PHP-Facebook-Api를 사용하고 있으며 사용자의 친구 목록을 얻는 방법을 알고 있습니다. 그것은과 같이 기본적으로 : 나는 사용자의 모든 친구를 표시하려는 HTTP 요청을마다 할 필요 없다 있도록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']; 
     } 
    } 

내 응용 프로그램에서 나는 기본적으로, 내 데이터베이스에있는 모든 친구를 저장합니다. 하지만 지금은 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 
    } 
    } 
}