How to get Likes Count when searching Facebook Graph API with search=xxx
Asked Answered
D

7

41

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?

Dwarf answered 19/7, 2013 at 21:25 Comment(0)
K
151

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.

Kernite answered 21/8, 2013 at 7:39 Comment(11)
Thanks a lot man..., U saved my time and also worry, Thanks a lotSalema
great answer and to get total likes of page just call graph.facebook.com/PAGE-IDForego
Thanks for this. Of course the Facebook API documentation is an utter travesty and never makes mention of a "summary" paramter >:( >:( >:(Nariko
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?Mukluk
Works great for me! Thanks! I too would like to know where is documented?Sievers
Documented here and this is not deprecated. This is in v2.6 tooRosendorosene
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 exampleCobos
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}}Grasp
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.Indian
How to get the access token?Coauthor
For anyone using v12: "likes" is now a nonexisting field and should be labeled "reactions" (i.e. reactions.limit(1).summary(true))Hallvard
G
23

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
               }
            },
       ...
Gleason answered 27/5, 2016 at 3:51 Comment(2)
How did you know about nested requests cant find anywhere in the documentationStephanotis
developers.facebook.com/docs/graph-api/using-graph-api => search for the word nested. Upvote if it was helpful :)Gleason
B
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

Bondman answered 9/8, 2013 at 15:35 Comment(4)
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 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?Atheroma
yes, i believe that is the only way. Or at least the only way i found.Bondman
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.Preview
If it helps anyone. To get the likes and comment summary in feed call goes like this " graph.facebook.com/PAGE-ID/…Burrow
H
8

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

search?q=xxx&fields=fan_count&type=page
Heyer answered 21/4, 2016 at 10:7 Comment(2)
to get the count of page likes you can use fan_count fieldHeyer
The /search endpoint is gone. You need to use /pages/search?q=XXX&fields=fan_countLissner
M
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)
Mountbatten answered 29/5, 2014 at 19:1 Comment(0)
E
1

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

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

Exuviate answered 26/5, 2017 at 10:42 Comment(0)
B
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)

Ballenger answered 16/1, 2020 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.