8

我对 Facebook 的 fql.multiquery 方法感到困惑。

我正在尝试检索帖子上的所有评论,然后还检索每个评论的用户信息。我可以毫无问题地获得评论,但我无法获得用户。

目前我正在使用以下内容:

  FB.api({
   method: 'fql.multiquery',
   queries: {
    query1: 'SELECT post_fbid, fromid, text, time FROM comment WHERE post_id="'+postID +'"',
    query2: 'SELECT id, name, url, pic FROM profile WHERE id IN (SELECT fromid FROM #query1)'
   }
  },
  function(response) {
  }
})

这给了我以下回应:

[
  {
    "name": "query1",
    "fql_result_set": [
      {
        "post_fbid": "xxxxx",
        "fromid": user1id,
        "text": "Here's a comment",
        "time": 1308579931
      },
      {
        "post_fbid": "xxxxx",
        "fromid": user2id,
        "text": "Another comment",
        "time": 1308580031
      }
    ]
  },
  {
    "name": "query2",
    "fql_result_set": [
      {
        "id": user1id,
        "name": "User 1 name",
        "url": "User 1 url",
        "pic": "User 1 pic"
      },
      {
        "id": user2id,
        "name": "User 2 name",
        "url": "User 2 url",
        "pic": "User 2 pic"
      }
    ]
  }
]

问题是,我不知道如何匹配这些!因此,我正在遍历评论,并希望打印每个评论的文本,并在其旁边显示用户名。我怎么做?

或者,有没有更好的方法来做到这一点?

4

2 回答 2

10

您可以通过循环评论并将 fromid 与用户响应中的 id 匹配来匹配这些结果。

例如:

    var comments = response[0].fql_result_set;
    var users = response[1].fql_result_set;    

    //loop through the comments
    for(var i = 0, j = comments.length; i<j; i++){

        for(var x = 0, y = users.length; x<y; x++){
             if(comments[i].fromid == users[x].id){
                 //we have a match, this comment is from user users[x]
                 //process users[x]
                 //break the inner for loop, since we already have a match
             }
        }

    }
于 2011-06-23T19:26:57.040 回答
0

如果你正在使用 PHP 并且需要一个注释数组来循环这个函数可能会派上用场:

public function getComments($objectID){
    $user = $comment =array();
    $q1 = "/fql?q=".urlencode("SELECT  id, fromid, text, time , likes FROM comment WHERE object_id ='$objectID' ");
    $res = $this->api($q1);
    $com = $res['data'];

    $q2 = "/fql?q=".urlencode("SELECT uid, name, username, pic_small, current_location FROM user WHERE uid IN (SELECT fromid FROM comment WHERE object_id ='$objectID' )");
    $res = $this->api($q2);
    $usr = $res['data']; 

    foreach($usr as $k=>$v){
        $user[$v['uid']] = $v;
    }
    foreach($com as $cmnt){
        $comment[$cmnt['id']] = $cmnt;
        $comment[$cmnt['id']]['user'] = $user[$cmnt['fromid']];
    }
     return $comment;
}

返回以 commentID 为键的单个评论数组:

Array(

[137194739772009_249649] => Array
    (
        [id] => 137194739772009_249649
        [fromid] => 1454592211
        [text] => Brilliant!
        [time] => 1357450854
        [likes] => 1
        [user] => Array
            (
                [uid] => 1454592211
                [name] => Jo Payne
                [username] => jo.payne.127
                [pic_small] => http://profile.ak.fbcdn.net/hprofile-ak-snc6/203035_1454592211_505092710_t.jpg
                [current_location] => Array
                    (
                        [city] => Pascoe Vale
                        [state] => Victoria
                        [country] => Australia
                        [zip] => 
                        [id] => 107340732634422
                        [name] => Pascoe Vale, Victoria, Australia
                    )

            )

    )

[137194739772009_252711] => Array
    (
        [id] => 137194739772009_252711
        [fromid] => 1734247348
        [text] => testing
        [time] => 1357531321
        [likes] => 0
        [user] => Array
            (
                [uid] => 1734247348
                [name] => Andreas Lustig
                [username] => andreaslustigcom
                [pic_small] => http://profile.ak.fbcdn.net/hprofile-ak-snc6/275058_1734247348_2025403101_t.jpg
                [current_location] => Array
                    (
                        [city] => Melbourne
                        [state] => Victoria
                        [country] => Australia
                        [zip] => 
                        [id] => 116190411724975
                        [name] => Melbourne, Victoria, Australia
                    )

            )

    )

)
于 2013-01-07T06:03:24.733 回答