如何使用 facebook php sdk 更快地查找朋友的信息

发布于 2024-10-04 00:08:01 字数 473 浏览 2 评论 0原文

我使用以下代码来检索用户的朋友列表以及这些朋友的信息。

$frnd = $facebook ->api('/me/friends?access_token='.$accessToken.'&fields=id,name,about,hometown');
for ($i=0; $i<$nr_friends; $i++)
  {
   $friendinfo = $facebook->api('/'.$frnd["data"][$i]["id"].'?fields=id,name,hometown,location');
   echo $friendinfo['name']." ".$friendinfo['location']['name']." ".$friendinfo['hometown']['name']."</br>";
  }

问题是,这种方法需要花费大量时间来加载好友信息。有什么办法可以让它更快吗?

I'm using the following code to retrieve the user's friends list and the info for those friends.

$frnd = $facebook ->api('/me/friends?access_token='.$accessToken.'&fields=id,name,about,hometown');
for ($i=0; $i<$nr_friends; $i++)
  {
   $friendinfo = $facebook->api('/'.$frnd["data"][$i]["id"].'?fields=id,name,hometown,location');
   echo $friendinfo['name']." ".$friendinfo['location']['name']." ".$friendinfo['hometown']['name']."</br>";
  }

The problem is, this method takes a lot of time to load the friends info. Is there any way to make this faster?

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

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

发布评论

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

评论(3

吖咩 2024-10-11 00:08:01

我认为您可以通过将 ID 作为 ids 参数传递来将好友信息的查询压缩到一个 API 中。我还没有测试过这个,但尝试类似以下内容:

// get a list of your your friends' IDs
$friends = $facebook->api('/me/friends?access_token='.$accessToken.'&fields=id');

// condense those IDs into a comma-separated string
$friends_ids = implode(',', $friends);

// now query for friends' name, hometown and location
$friends_info = $facebook->api('/?access_token='.$accessToken.'&fields=id,name,hometown,location&ids='.$friends_ids);

我不确定确切的语法,没有测试自己,但你应该在这里找到更多信息:http://developers.facebook.com/docs/api#reading

I think you can condense the querying of friends' info into one API by passing the IDs as an ids parameter. I haven't tested this, but try something similar to the following:

// get a list of your your friends' IDs
$friends = $facebook->api('/me/friends?access_token='.$accessToken.'&fields=id');

// condense those IDs into a comma-separated string
$friends_ids = implode(',', $friends);

// now query for friends' name, hometown and location
$friends_info = $facebook->api('/?access_token='.$accessToken.'&fields=id,name,hometown,location&ids='.$friends_ids);

I'm not sure on the exact syntax without testing myself, but you should find more information here: http://developers.facebook.com/docs/api#reading

沉鱼一梦 2024-10-11 00:08:01
<?php
    $friends = $facebook->api('me/friends');

    //print_r($friends['data']);
    print_r("Number of Friends: ". count($friends['data']));

    foreach ($friends['data'] as $key=>$listOfFriends) {
        echo "<br/>".$key." ".$listOfFriends['name']."<img src='https://graph.facebook.com/".$listOfFriends['id']."/picture' width='50' height='50' title='".$listOfFriends['name']."' />";     
    }

?>
<?php
    $friends = $facebook->api('me/friends');

    //print_r($friends['data']);
    print_r("Number of Friends: ". count($friends['data']));

    foreach ($friends['data'] as $key=>$listOfFriends) {
        echo "<br/>".$key." ".$listOfFriends['name']."<img src='https://graph.facebook.com/".$listOfFriends['id']."/picture' width='50' height='50' title='".$listOfFriends['name']."' />";     
    }

?>
冷心人i 2024-10-11 00:08:01

使用 FQL 语言:

$facebook->api(array('method'=>'fql.query','query'=>"SELECT uid,name,first_name,middle_name,last_name,pic_square,hometown_location,current_location,profile_url,email,website FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"));

有关所有表及其相应列名称的列表,我参考:Facebook FQL 参考

Use the FQL language:

$facebook->api(array('method'=>'fql.query','query'=>"SELECT uid,name,first_name,middle_name,last_name,pic_square,hometown_location,current_location,profile_url,email,website FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"));

For a list of all the tables with their corresponding column names I refer to: Facebook FQL Reference

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