13

I use graph api to get the picture's comments, but I want to first sort the results by creating time and then return to the latest data. Similar to the sql statement 'order by create_time desc', I do not know if have such a parameter.

Currently used to offset and limit access to the latest data, but also know the total number of comments,

pagesize = 25;
offset = comments.count - pagesize;
limit = 25;

url = "https://graph.facebook.com/" + object_id + "/comments?access_token=" + access_token + "&limit=" + limit + "&offset=" + limit;

next page:

offset -= 25

but comments.ount of numerical sometimes is not accurate

and the result of the request URL to return to sometimes don't match

Whether to have very good solution

Or I used the wrong way (‘limit’ and ‘offset’ Parameter)!!!


Thank you for your answer.

"Graphics API" the existence of the cache?

i post a message and 46 comments.requests url, set the parameters:

offset=0&limit=1

Then it should return to the last comment (latest one), the actual return to the middle of a comment, and I tested a few times, set the offset and limit. According to the returned results, the middle one is the latest comment

If I set the limit value is greater than the 'comment.count', the returned data is all, the official website and facebook consistent

Because the cache reason?

Thanks again~

mast
  • 131
  • 1
  • 1
  • 4
  • why don't you use FQL? seems like more flexible in this case. – Anatoly Lubarsky Oct 12 '11 at 15:53
  • fql~ **SELECT object_id, text, time FROM comment WHERE object_id = 'object_id' order by time desc** Indeed very flexible, but "graph api" does not it? I can only use it~. 'fql' is a "Graph api" ? If that would be too nice~! – mast Oct 12 '11 at 16:16
  • graph API doesn't have WHERE and ORDER BY so in your case FQL seems more flexible. FQL is a feature of graph since it performs queries on graph objects and connections. – Anatoly Lubarsky Oct 12 '11 at 23:13
  • This has become important again as FQL seems to be deprecated. https://developers.facebook.com/docs/reference/fql/ – Josiah Mar 16 '15 at 22:41

2 Answers2

5

@dbau - You are still better off using FQL. In my experience, unless you are making a very simple call, you have very little control over what you get via a Graph API call.

Why don't you want to use FQL? FQL is an endpoint of the Graph API. There is still some data that can only be returned via FQL.

This will get you the result you're looking for. The query needs to be URL encoded. I left it in plain text for clarity.

 https://graph.facebook.com/fql?access_token=[TOKEN]&q=
    SELECT id, fromid, text, time, likes, user_likes FROM comment
      WHERE object_id = [OBJECT_ID] ORDER BY time DESC LIMIT 0,[N]

You may find you don't get [N] comments returned each time, because Facebook filters out items that are not visible to the access_token owner after the query is run. You could either up the LIMIT and filter out any excess results returned or if you are using a user access_token, you could add AND can_like = TRUE to the WHERE clause to be guaranteed that, if they exist, [N] posts visible to the current user are returned.

cpilko
  • 11,792
  • 2
  • 31
  • 45
3

Graph API returns latest objects first.

Facebook provides 2 keywords to filter the fetched data.

  1. Limit : Returns "limit" number of latest records
  2. Offset : Returns "limit" number of records from the offset position

So to retrieve latest "x" comments posted for an object

https://graph.facebook.com/[OBJECTID]?limit=[X]&offset=0

To retrieve next "X" comments (page wise)

https://graph.facebook.com/[OBJECTID]?limit=[X]&offset=[X*PAGENo]

Hope the answer is clear enough for you.

Robin
  • 2,339
  • 1
  • 15
  • 11
  • thank you for your answer. "Graphics API" the existence of the **Cache**? – mast Oct 12 '11 at 16:48
  • 1
    Is this still the case? *offset=0* seems to refer to the oldest comment while *offset=1...n* returns later comments for me! – dbau Jul 24 '12 at 11:15
  • See my updated answer. Facebook GRAPH API retrieves latest records first by default and same is the case with the normal Facebook website. So offset zero retrieves the most latest "LIMIT" number of records – Robin Aug 04 '12 at 10:13
  • Is there a way to reverse the order so you get the oldest ones first? – chacham15 Dec 15 '12 at 08:44
  • 9
    The claim that Graph API returns items newest-first is SimplyNotTrue™ at least in the case of comments on a photo, and possibly in a few other cases. These are always returned oldest-first, and the "until" modifier is flatly ignored. – Szczepan Hołyszewski Feb 18 '13 at 20:57
  • 2
    Retrieving photos from the Graph API also returns oldest-first. – Zach Lysobey Dec 12 '13 at 22:06