如何检索粉丝页面的所有评论

时间:2012-01-11 12:50:00

标签: facebook facebook-fql fql.multiquery

我需要获得Fan Page的所有评论。不仅来自墙壁,还来自相册,照片,笔记等。

我以这种方式解决了这个问题。首先,我获取Fan Page的所有对象(专辑,备注等)。其次,我使用FQL多查询来对每个对象进行评判。

    // Import comment list
    $facebook = \Yii::app()->facebook->setUserAccessToken();
    $commentsColumns = array(
        'xid', 'object_id', 'post_id', 'fromid', 'time', 'text', 'username', 
        'reply_xid', 'post_fbid', 'app_id', 'likes', 'comments', 'user_likes', 
        'is_private', 'id',
    );
    $offset = 0;
    $limit = 30;
    while (($offset <= count($objectIdList)) && ($offset <= count($postIdList))) {
        $currentObjectIdList = array_slice($objectIdList, $offset, $limit);
        $currentPostIdList = array_slice($postIdList, $offset, $limit);
        $multiQuery = array(
            'comments' =>
                ' SELECT '.implode(', ', $commentsColumns).' '.
                ' FROM comment '.
                ' WHERE ((object_id IN ('.implode(',', $currentObjectIdList).')) '.
                ' OR (post_id IN ('.implode(',', $currentPostIdList).'))) '.
                ' AND (time >= ' . $lastCommentTime . ') ' .
                ' ORDER BY time DESC '.
                ' LIMIT 1000 ',
            'users' => 
                ' SELECT uid, name ' .
                ' FROM user ' .
                ' WHERE uid IN (SELECT fromid FROM #comments) ',
            'pages' => 
                ' SELECT page_id, name ' .
                ' FROM page ' .
                ' WHERE page_id IN (SELECT fromid FROM #comments) '
        );
        $multiQueryResult = $facebook->api(array(
            'method'   => 'fql.multiquery',
            'queries'  => $multiQuery
        ));

        // Parse multi query results
        $from = array();
        foreach ($multiQueryResult as $result) {
            switch ($result['name']) {
                case 'comments':
                    $commentList = $result['fql_result_set'];
                    break;
                case 'users':
                case 'pages':
                    foreach ($result['fql_result_set'] as $set) {
                        switch ($result['name']) {
                            case 'users':
                                $from[$set['uid']] = $set['name'];
                                break;
                            case 'pages':
                                $from[$set['page_id']] = $set['name'];
                                break;
                        }
                    }
                    break;
            }
        }

        // Save comments to local DB
        foreach ($commentList as $commentData) {
            $comment = new Comment();
            $comment->fbId          = $commentData['id'];
            $comment->fbPageId      = $this->fbId;
            $comment->from          = array(
                'id'    => $commentData['fromid'],
                'name'  => isset($from[$commentData['fromid']]) ? $from[$commentData['fromid']] : null,
            );
            $comment->message       = $commentData['text'];
            $comment->created_time  = $commentData['time'];
            $comment->likes         = $commentData['likes'];
            $comment->save();
        }

        // Next step
        $offset = $offset + $limit;
    }

此解决方案有效,但速度不是很快。有谁知道更好的解决方案?

1 个答案:

答案 0 :(得分:0)

我认为您可以使用下面给出的图API方法, https://graph.facebook.com/userid/feed获取用户的所有提要。它将返回一个json数组,从那里你可以收集评论,状态等等。