40

I am currently using Facebook graph api search to search posts as

http://graph.facebook.com/search?q=iwatch&type=post&access_token=xxxxx 

It returns in JSON format fields and use to include the like:count for a given post.

After reading the dev roadmap (https://developers.facebook.com/roadmap/) for changes after July 10th I am instructed to use the summary=true param but I have no idea how to get this to work with search?

From FB blog on roadmap.

Removing 'count' from 'comments' Graph API connection We are removing the undocumented 'count' field on the 'comments' connection in the Graph API. Please request {id}/comments?summary=true explicitly if you would like the summary field which contains the count (now called 'total_count')

I have tried various combinations and searched for examples but no dice. Can anyone give me some advice on how to get the new summary=true to work within a search URL for searching posts?

Kara
  • 6,115
  • 16
  • 50
  • 57
Digby Norris
  • 401
  • 1
  • 4
  • 5

7 Answers7

149

Couldn't find this in the documentation but multiple calls to the API are not necessary. You can use summary when querying a feed or multiple posts. Specify this in the fields parameter.

https://graph.facebook.com/PAGE_ID/feed?fields=comments.limit(1).summary(true),likes.limit(1).summary(true)

This will return a response like this.

{
  "data": [
    {
      ....
      "summary": {
        "total_count": 56
      }
      ...
    }, 
    {
      ....
      "summary": {
        "total_count": 88
      }
      ...
    }
  ]
}

This will be much faster than making individual requests for each object just to get the number of comments or likes.

dvk
  • 1,907
  • 1
  • 11
  • 11
  • Thanks a lot man..., U saved my time and also worry, Thanks a lot – Nagaraj Nov 21 '13 at 15:47
  • great answer and to get total likes of page just call https://graph.facebook.com/PAGE-ID/ – Adeem Mar 20 '14 at 04:31
  • 7
    Thanks for this. Of course the Facebook API documentation is an utter travesty and never makes mention of a "summary" paramter >:( >:( >:( – Brade May 12 '14 at 15:07
  • Thanks a lot! Can I ask how did you find out about this? If it's not documented, should we expect for it to be deprecated? – p4sh4 Oct 21 '14 at 11:14
  • Works great for me! Thanks! I too would like to know where is documented? – Pierre Sep 17 '15 at 02:35
  • Documented [here](https://developers.facebook.com/docs/graph-api/reference/photo/likes/) and this is not deprecated. This is in `v2.6` too – RaviRokkam Apr 22 '16 at 12:44
  • There is any reason why this work with the graph query URL and not when using the sdk (android, php, etc)? If i set /id of post/ and the parameters "fields" "/likes.limit(1).summary(true)" I don't get data. It only works with "/id of post/likes" with "summary=total_count" as params, for example – Mariano Zorrilla Apr 25 '16 at 00:44
  • How do you set `summary=true` with an API? such as the PHP one? my request looks like this: $pageId . "?fields=feed{message,description,type,icon,link,likes{total_count}} – Buffalo Jul 04 '16 at 15:43
  • that you very much :) I didn't know I could put `.limit(0-9)` on the end of the fields. Now I can get more than 25 likes for each post without doing 2 calls. – mic Mar 16 '17 at 23:01
  • How to get the access token? – Enrico May 04 '21 at 17:46
  • For anyone using v12: "likes" is now a nonexisting field and should be labeled "reactions" (i.e. reactions.limit(1).summary(true)) – Daerik Jan 06 '22 at 16:46
22

You can also get all Posts > Comments > Likes in a single request:

https://graph.facebook.com/<obj_id>/feed?fields=message,comments.limit(10).summary(true){message,from,likes.limit(0).summary(true)}

The braces are nested requests.

This gives the following result:

{
    "data": [
      {
        "message": "Contents of the Post"
        "id": "123456789123456789",
        "comments": {
        "data": [
          {
            "message": "Contents of the Comment",
            "from": {
                 "name": "John Doe",
                 "id": "123456789"
            },
            "likes": {
               "data": [],
               "summary": {
                  "total_count": 14,
                  "can_like": true,
                  "has_liked": false
               }
            },
       ...
Nicomak
  • 2,319
  • 1
  • 21
  • 23
  • How did you know about nested requests cant find anywhere in the documentation – vumaasha Jul 11 '16 at 08:57
  • 4
    https://developers.facebook.com/docs/graph-api/using-graph-api => search for the word `nested`. Upvote if it was helpful :) – Nicomak Jul 11 '16 at 09:39
10

The summary is on the likes connection of the post object

just call

https://graph.facebook.com/POST_ID/likes?summary=true&access_token=XXXXXXXXXXXX

there will be a 'summary' element with a 'total_count' field

Jon
  • 766
  • 1
  • 9
  • 27
  • I also see the FB document about this by setting "summary=1" to get the like count. However, do you know how to get the like count when I get the page feeds by https://graph.facebook.com/PAGE_ID/feed If I can't get the like counts while getting the feed, then I have to make individual request for each post just for the like count... is this the only way out? – Joe Huang Aug 11 '13 at 13:17
  • yes, i believe that is the only way. Or at least the only way i found. – Jon Aug 12 '13 at 07:29
  • This worked for me. I had to make multiple calls to the api. I also added `limit=SOME_HUGE_NUMBER` so that it wouldn't paginate/limit the number that showed, but I'm not sure if that was necessary. – Daniel Aug 13 '13 at 17:06
  • If it helps anyone. To get the likes and comment summary in feed call goes like this " https://graph.facebook.com/PAGE-ID/feed?fields=likes.limit(1).summary=true,comments.limit(1).summary=true&access_token=XXXXXXXXXXXX – Narayan Singh Mar 11 '16 at 09:39
8

To get the count of page likes you can use fan_count field.

search?q=xxx&fields=fan_count&type=page
Shush
  • 99
  • 1
  • 1
5

I construct my API query like this, and it allows me to fetch a one shot query:

https://graph.facebook.com/PAGE_ID/feed?fields=comments.limit(25).summary(true),likes.limit(25).summary(true)
Matt
  • 74,352
  • 26
  • 153
  • 180
1

the api has changed. new field name is 'fan count'.

https://graph.facebook.com/PAGE_ID?fields=fan_count

Pirkka Esko
  • 718
  • 9
  • 13
0

After some tinkering, I found a simple solution.

I found you can get the number of likes a page has with Facebooks Graph API accessing the url:


https://graph.facebook.com/v5.0/{page-id}?fields=fan_count&access_token={user-access-token}

You can curl it for the same result.


curl -i -X GET \
 "https://graph.facebook.com/v5.0/{page-id}?fields=fan_count&access_token={user-access-token}"

(replace page-id and user-access-token, with your own)

TriThomas
  • 383
  • 2
  • 16