Facebook Graph API 获取所有用户及其状态

发布于 2024-10-28 01:37:51 字数 1123 浏览 1 评论 0原文

我正在使用 Facebook Graph API,我想知道是否可以通过一次调用获取所有用户及其当前状态的列表?

我基本上想要与 https://graph.facebook.com/me/friends 相同的结果,但是每个对象都包含当前状态。

感谢您的帮助!

编辑:

这里有一些澄清。如果 https://graph.facebook.com/me/friends?access_token=... 给我这个:(又名我所有朋友的列表)

{
  "data": [
    {
      "name": "Foo",
      "id": "1"
    },
    {
      "name": "Bar",
      "id": "2"
    },
    {
      "name": "Foo Bar",
      "id": "3"
    },
    {
      "name": "Bar Foo",
      "id": "4"
    }
  ]
}

什么 URL 或 FQL 会给我这个:(又称我所有朋友及其状态的列表)

{
  "data": [
    {
      "name": "Foo",
      "id": "1"
      "status": "This is my current status!"
    },
    {
      "name": "Bar",
      "id": "2"
      "status": "This is my current status!"
    },
    {
      "name": "Foo Bar",
      "id": "3"
      "status": "This is my current status!"
    },
    {
      "name": "Bar Foo",
      "id": "4"
      "status": "This is my current status!"
    }
  ]
}

我只想打一个电话,因为与我不同的是,大多数人有 100 多个朋友,而且我不想仅仅为了获取状态而打 100 多个电话。

I am using the Facebook Graph API and I was wondering if there was anyway to get a list of all the users and their current status in one call?

I basically want the same results as https://graph.facebook.com/me/friends but with the current status included per object.

Thanks for any help!

EDIT:

Here is some clarification. if https://graph.facebook.com/me/friends?access_token=... gets me this: (AKA a list of all my friends)

{
  "data": [
    {
      "name": "Foo",
      "id": "1"
    },
    {
      "name": "Bar",
      "id": "2"
    },
    {
      "name": "Foo Bar",
      "id": "3"
    },
    {
      "name": "Bar Foo",
      "id": "4"
    }
  ]
}

what URL or FQL will get me this: (AKA a list of all my friends and their statuses)

{
  "data": [
    {
      "name": "Foo",
      "id": "1"
      "status": "This is my current status!"
    },
    {
      "name": "Bar",
      "id": "2"
      "status": "This is my current status!"
    },
    {
      "name": "Foo Bar",
      "id": "3"
      "status": "This is my current status!"
    },
    {
      "name": "Bar Foo",
      "id": "4"
      "status": "This is my current status!"
    }
  ]
}

I only want to make one call, because unlike me, most people have 100+ friends, and I don't want to have to make 100+ calls just to get statuses.

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

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

发布评论

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

评论(4

神经大条 2024-11-04 01:37:51

虽然新的 Batch API 应该可以满足您的要求,但我做不到设法使其正常工作。看来还是有bug。

现在可以使用 fql.multiquery,这是一个简单的示例:

<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET"; 
$my_url = "REDIRECT_URL";

$code = $_REQUEST["code"];

if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
  . $app_id . "&redirect_uri=" . urlencode($my_url);

echo("<script> top.location.href='" . $dialog_url 
  . "'</script>");
}

$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) 
. "&client_secret=" . $app_secret 
. "&code=" . $code;

$access_token = file_get_contents($token_url);

$queries = '{"query1":"SELECT uid,name FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1=me())","query2":"SELECT uid,message FROM status WHERE uid IN (SELECT uid FROM #query1)"}';
$multiquery = "https://api.facebook.com/method/fql.multiquery?queries=" . urlencode($queries) . "&format=json&" . $access_token;
$res = json_decode(file_get_contents($multiquery), TRUE);

$final = array();
foreach( $res[1]['fql_result_set'] as $i=>$message_arr ) {
    foreach( $res[0]['fql_result_set'] as $j=>$friend ) {
        if( $message_arr['uid'] == $friend['uid'] ) {
            $friend['message'] = $message_arr['message'];
            $final[] = $friend;
            break;
        }
    }
}
print_r($final);
?>

这里我们在第一个查询中获取用户 ID 和名称,并从第二个查询中获取状态消息。
现在,这将在第一个查询或第二个查询中返回更少的结果!因为:

  • 朋友可能在过去 30 天内没有发布过状态消息,因此它不会出现在第二个查询中(第二个查询的结果集较少)。
  • 一位朋友可能在过去 30 天内发布了多条状态消息(它应该全部返回 - 最多 50 条?!!)我不确定,但如果是这种情况,那么第二个将/应该返回比第一个更多的结果。所以我只收到一条消息(注意中断)。

While the new Batch API should do what you are asking for, I couldn't manage to make it work properly. It seems that it's still buggy.

Now to achieve this with fql.multiquery, here's a quick example:

<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET"; 
$my_url = "REDIRECT_URL";

$code = $_REQUEST["code"];

if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
  . $app_id . "&redirect_uri=" . urlencode($my_url);

echo("<script> top.location.href='" . $dialog_url 
  . "'</script>");
}

$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) 
. "&client_secret=" . $app_secret 
. "&code=" . $code;

$access_token = file_get_contents($token_url);

$queries = '{"query1":"SELECT uid,name FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1=me())","query2":"SELECT uid,message FROM status WHERE uid IN (SELECT uid FROM #query1)"}';
$multiquery = "https://api.facebook.com/method/fql.multiquery?queries=" . urlencode($queries) . "&format=json&" . $access_token;
$res = json_decode(file_get_contents($multiquery), TRUE);

$final = array();
foreach( $res[1]['fql_result_set'] as $i=>$message_arr ) {
    foreach( $res[0]['fql_result_set'] as $j=>$friend ) {
        if( $message_arr['uid'] == $friend['uid'] ) {
            $friend['message'] = $message_arr['message'];
            $final[] = $friend;
            break;
        }
    }
}
print_r($final);
?>

Here we are getting the user id and name in the first query and getting the status messages from the second one.
Now this would return fewer results in the first query or the second! because:

  • A friend may have not posted a status message for the last 30 days, so it won't appear in the second query (second has fewer result set).
  • A friend may have posted more than one status message in the last 30 days (it should return them all - up to 50 ?!!) I'm not sure, but if this is the case then the second will/should return more results than the first one. So I'm getting only one message (notice the break).
伤感在游骋 2024-11-04 01:37:51

尝试以下 fql:

Select uid, message from status where uid in (select uid2 from friends where uid1 = [active user id])

只需确保限制为特定日期,因为默认情况下这将返回最近 50 次更新/或最近 30 天的活动。

Try the following fql:

Select uid, message from status where uid in (select uid2 from friends where uid1 = [active user id])

Just make sure to limit to a specific date, because this will return the last 50 updates / or last 30 days of activity by default.

吃颗糖壮壮胆 2024-11-04 01:37:51

您好,您只需要获得user_status 或friend_status的扩展许可即可进行此操作以及任何调用me 对象将获取用户当前状态...

json_decode(file_get_contents('https://graph.facebook.com/me/friends?access_token='.$cookie['access_token']));

hi you just need to take extended permission of user_status or friend_status for this and any call on me object will grab the user current status...

json_decode(file_get_contents('https://graph.facebook.com/me/friends?access_token='.$cookie['access_token']));
鹿! 2024-11-04 01:37:51

感谢 @mgilson 的回答,这可以合并为一个 FQL 语句,如下所示:

从状态 WHERE uid IN 中选择 status_id、uid、消息(从用户 WHERE uid IN 中选择 uid(从朋友 WHERE uid1=me()) 中选择 uid2)

我发现的神奇之处在于请求 friends_status 权限,这将从公共帖子向朋友和其他人开放,这有助于复制墙的感觉。

Thanks to @mgilson for his answer, this can be combined to one FQL statement like this:

SELECT status_id,uid,message FROM status WHERE uid IN (SELECT uid FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1=me()))

The magic I found is requesting the friends_status permission which will open up from Public posts to Friends and others which help replicate the feeling of the wall.

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