简体   繁体   中英

Using the Facebook graph API to retrieve comments for statuses in REVERSE chronological order

I'm trying to retrieve comments for statuses from Facebook's Graph API in REVERSE chronological order, that is the newest comments should be on top. I am not sure if this is even possible.

I know Facebook orders posts in chronologically descending order by default, and comments are chronologically ascending (oldest on top) by default. By default, I mean in Facebook.com and the way they are retrieved from the API.

Using the Graph API I am able to retrieve statuses in descending order, but comments are returned in ascending order. I'd like to get the comments in reverse order from the API without having to sort them in my program. The reason being is that the program is limited to the # of comments it will download, so setting the limit=xx will actually retrieve the first xx comments, rather than the newest xx comments. This is no good for me because if a status has 500 comments, and xx=100, it will always retrieve the same set of comments (first 100 to be posted).

The best parameter for the Html request that I can find is 'order_by' which looks for a time/date param (updated_time), but this does not actually work since it is already ordered by the updated_time (but ascending). I would like to specify order_by descending updated_time, if possible.

Any help will be appreciated.

There is no direct way of doing this with the API but one way to get this working is to retrieve all the comments for the status, assign them to an array, and loop through the results in reverse order.

The below example gets the last status update from the Guinness Ireland page and assigns all the messages into an array. Once we have this array we can spit out the results with a loop.

Example:
$data = file_get_contents('https://graph.facebook.com/GuinnessIreland/statuses?limit=1&access_token=' . $ACCESS_TOKEN);

$comments = json_decode($data);
$messages = ($comments->data[0]->comments->data);

for ($counter = count($messages); $counter >= 0; $counter = $counter - 1) {
echo $messageArray[$counter]->message;
echo $messageArray[$counter]->created_time;
}

You can get the comments using get request through this url.. you will get json response.

https://graph.facebook.com/ "StatusID"/comments?limit=500&access_token="accessToken"

StatusID=132343454_54346345 userID statusid accessToken=user Access token.

you ll get JSOn response. get all the details like user who commented, commentid, message... you will get JSON response . Parse them.check the comment on next page by connecting url in "next" string in the "page" object at the bottom of the JSON Page. Enjoy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM