How to retrieve all comments of the Fan Page

1.4k views Asked by At

I need to get all comments of the Fan Page. Not only from the wall, but from the albums, photos, notes, etc.

I solved this problem in such a way. First I get all objects (albums, notes, etc.) of the Fan Page. Second I get commets to each object, using FQL multi query.

    // 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;
    }

This solution works, but is not pretty fast. Has anyone know better solution?

1

There are 1 answers

2
Suj On

I think you can use the graph API method given below, https://graph.facebook.com/userid/feed to get all the feeds of the user .It will return an json array,from there you can collect the comments,statuses etc etc