PHP / Facebook-Api:更新好友列表

发布于 2024-10-08 19:17:35 字数 959 浏览 0 评论 0原文

我正在使用官方 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请求。但现在我想更新好友列表。我不想删除所有朋友联系并重新保存它们。那么有人知道一个选项,如何获取自特定日期以来好友列表的更改吗?

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 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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海之角 2024-10-15 19:17:35

那么你必须检查它们是否在数据库中。我建议您获取数据库中所有用户 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
     }
  }
}

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
     }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文