Facebook graph api comment count
Asked Answered
M

9

21

seems Facebook changed the result of posts, few weeks ago it was possible to read the comment count from the post directly

https://graph.facebook.com/125909647492772_502974003098530

you got something like

...
"comments": {
"data": [
  {
    "id": "502974003098530_78616446", 
    "from": {
      "name": "Mathias Fritz", 
      "id": "526559276"
    }, 
    "message": "saugeil!", 
    "can_remove": false, 
    "created_time": "2013-03-26T14:58:01+0000", 
    "like_count": 1, 
    "user_likes": false
  }
], 
"paging": {
  "cursors": {
    "after": "MQ==", 
    "before": "MQ=="
  }
}, 
"count": 1

but now the count is missing.

I did some research on the graph documentation but the only change in that direction seems to be that comments can have comments now... and those comments are counted in a field named comment_count.

Is there still a way to get the total comment count?

Maliamalice answered 3/5, 2013 at 11:55 Comment(4)
It should still be possible to do this with FQL, check out the answers to this question: #14372449Blackfellow
Thank you very much! SELECT comment_info FROM stream WHERE post_id = ... works. If you post an answer I could accept it.Maliamalice
@Maliamalice How can i get postId which you use like "125909647492772_502974003098530" ?Quipster
@HarshalKalavadiya there are a few different ways to get the postid, depending on your approach, just google for "facebook postid"Maliamalice
O
35

To get the count, add ?summary=1 at the end: https://graph.facebook.com/125909647492772_502974003098530/comments?summary=1

Obstipation answered 3/9, 2013 at 11:18 Comment(2)
Indeed. So far this is the only way I've found to get likes/comments totals without without having to page through all of the results when you don't have an access token. Thank you!Forsberg
There's another answer that shows how you can do this via ?fields=likes.summary(true) from the list page, rather than getting the details for each post individuallySki
E
22

I was having same problem, just adding likes.summary(true),comments.summary(true) in parameter in against "fields" worked for me.

e.g. I used https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN&fields=story,from,story_tags,likes.summary(true),comments.summary(true)

instead of https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN

Also you can add other parameters if you want; separated by a ,

Endamage answered 21/11, 2013 at 17:26 Comment(0)
C
17

summary=true is what you are looking for

Get likes count :

114916098537132_1265715836790480/likes?summary=true

Get comments count

114916098537132_1265715836790480/comments?summary=true

Get shares count :

114916098537132_1265715836790480?fields=shares

And last [ combining all 3 ]

114916098537132_1265715836790480?fields=shares,likes.summary(true),comments.summary(true)

Improved version ( add limit(0) to removes list of likes and get only summary ):

114916098537132_1265715836790480?fields=shares,likes.limit(0).summary(true),comments.limit(0).summary(true)
Cress answered 3/5, 2016 at 7:1 Comment(3)
Is there a way to get details about shares about author and time of sharing as it can be done for likes and comments?Wormwood
@Wormwood sorry mate, not aware of that :)Cress
Hi @AbhishekGoel I have used last one, but it is only giving correct numbers for shares, For comments and likes it always giving less numbers. Do you have any idea about it?Chifley
M
8

This works perfectly with me:

fields=shares,created_time,comments.summary(true).limit(0)

This return comments count at summary and return 0 comments at the same time which is perfect as you only need the comment count.

Metalliferous answered 5/10, 2017 at 3:59 Comment(1)
You are my hero.Backup
B
5

You can get total comment count via FQL. See this question below as reference:

Facebook API - comment count via FQL

Here's the query you need: SELECT comment_info FROM stream WHERE post_id = ...

Blackfellow answered 6/5, 2013 at 13:52 Comment(5)
Yes, this is still working. It sounds like you may have run into a permissions issue. Make your you've requested the read_stream permission.Blackfellow
this gives incomplete data because of the behavior of the fb stream object. You have to use "limit" and "order by" to use it correctly. I would prefer the solution from Veerle Struyf.Noontime
FB just announced a couple weeks ago that FQL has been deprecated and will not be available after the next couple API releases.Boustrophedon
Please change the accepted answer to @Veerle Struyf answer. This method is now deprecated.Cathee
Sorry, I lost track of this question, since I no longer work on the project using the facebook api. The answer seems correct so I changed the accepted answer. Thanks for the info @NicStrongMaliamalice
G
3

If you'd like to count everything on Facebook. (That number is visible for Facebook's User)

You should use FQL (Facebook Query Language) instead of Graph API.

Facebook Query Language Reference

This situation you should to query

SELECT comment_info FROM stream WHERE post_id = ...
Goldcrest answered 6/5, 2013 at 14:48 Comment(0)
G
3

You can do such requests:

{pageid}/posts?fields=comments.summary(1){id}

It will return a list of posts including comments count of each post. Here I have returned only the comment id because I only need the number of comments per post, but of course you can include many other fields:

{pageid}/posts?fields=comments.summary(1){id,message},id

Gully answered 23/1, 2015 at 9:24 Comment(0)
H
-1

Or to make less changes to you excisting code, use:

    $.each(json.data,function(i,fb){
    ...
       var commentsCount = 0
       if(fb.comments!=undefined){
          commentsCount=fb.comments.data.length
       }
    ...
    }

commentsCount holds number of comments for active child

Hospital answered 11/7, 2013 at 16:44 Comment(1)
This wont work well since you get comments paged. You only get 25 comments per call after which you would need to call the next page.Maliamalice
T
-1

Try the following:

{
    "data": [
        {
            "id": "447235535389660_1226199",
            "from": {
                "name": "Harjeet Walia",
                "id": "100004980601083"
            },
            "message": "Price",
            "can_remove": false,
            "created_time": "2013-09-06T10:39:01+0000",
            "like_count": 0,
            "user_likes": false
        },
        {
            "id": "447235535389660_1226152",
            "from": {
                "name": "Shoba Dhyani Jakhmola",
                "id": "100000906896060"
            },
            "message": "baap re kitna mehnga !",
            "can_remove": false,
            "created_time": "2013-09-06T10:05:09+0000",
            "like_count": 0,
            "user_likes": false
        }
    ],
    "paging": {
        "cursors": {
            "after": "MQ==",
            "before": "NA=="
        }
    }
}

then

int commentCount = <JsonNode Var with above data>.path("comments").path("data").size();

Here commentCount will give the number of comments.

Tacet answered 6/9, 2013 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.