Follow this Blog!

Jun 30, 2015

f Comment

[SOLVED] Using Facebook Graph API To Crawl Comments from a Facebook Comments Plugin

Amazon Let's say you put on your website a Facebook comments plugin that look like https://developers.facebook.com/docs/plugins/comments. How do you programmatically retrieve all the comments posted in this social plugin? In other words, how do you use Facebook's Graph API to get every comment posted in the FB comments plugin?

Why would you want to do that? Well you may want to scrape all the comments left in your comments plugin and store them in your database so you can make use of them somehow.

First let's get some definitions straight:

A top-level comment is created as a brand-new comment and is not created in response to another comment.
A sub comment is created in response to another comment.

Q: How do you get every comment sorted in ascending order of created time?

Use this URL:

http://graph.facebook.com/comments?id=http://www.chtoen.com/&limit=10&filter=stream

id is the URL where your comments plugin points to.
limit is the number of comments to return. If there are more comments, the returned JSON will give you the URL to go to the next page.
filter=stream tells Graph API to return results in the ascending chronological order, but it seems to work only for a portion of the comments.

This URL returns every comment in a JSON structure, including top-level comments and sub comments.

Q: How do you get every top-level comment?

Use this URL:

http://graph.facebook.com/comments?id=http://www.chtoen.com/&limit=10

id is the URL where your comments plugin points to.
limit is the number of comments to return. If there are more comments, the returned JSON will give you the URL to go to the next page.

This URL only returns top-level comments.

Q: Given the ID of a top-level comment, how do I find its sub comments?

Let's say you'd like to find all sub comments of a top-level comment whose ID is 281398621990351_688438464619696. Simply use the following URL to give you all its children comments:

https://graph.facebook.com/281398621990351_688438464619696/comments?access_token=<your access token>

To find your access token, consult Fixing Facebook Graph API Error: "An access token is required to request this resource.".

If you see "message_tags" in the returned JSON structure, it means the message contains a reference to another Facebook entity. In this example, Vivi Lin is a reference in the message.
{
    "id": "281398621990351_684729051657304",
    "can_remove": false,
    "created_time": "2015-06-06T04:13:07+0000",
    "from": {
        "id": "3624019",
        "name": "Michael Wen"
    },
    "like_count": 0,
    "message": "Vivi Lin \u6211\u73fe\u5728\u624d\u770b\u5230\u4f60\u7684\u554f\u984c\u54c8\u54c8...\nI have 14 PTOs a year.\nPTO \u5c31\u662f\u6307\u6709\u85aa\u6c34\u7684\u5047",
    "message_tags": [
        {
           "id": "100000276240501",
           "length": 8,
           "name": "Vivi Lin",
           "offset": 0,
           "type": "user"
        }
    ],
    "user_likes": false
}

Q: What about crawling posts of a Facebook fan page?

To find out how to crawl every post of an FB fan page, consult How do You Use Facebook API to Get Every Post of a Facebook Fan Page.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael