How to get facebook share, like, comment count for a url with graph api only (in a non-deprecated way)
Asked Answered
K

6

13

The problem is that after v2.1 of graph api fql will be deprecated.

Can someone tell me how to get separate likes, share count for given url using only graph api? I was looking for some documentation, but there isn't any, i can get only total "shares" which is likes + shares + comments.

Yes i know there is immortal http://api.facebook.com/restserver.php?method=links.getStats&urls, but what if facebook shuts it down at last?

Kyungkyushu answered 19/9, 2014 at 18:38 Comment(3)
This question is a duplicate, see here #25830974Delgadillo
No it is not, i don't any likes/share count with official graph api call there, if I'm mistaken, please show me the answer.Kyungkyushu
Not quite immortal after all (REST API is deprecated for versions v2.1 and higher (12))Ungual
C
12

The REST API (with calls like http://api.facebook.com/restserver.php?method=links.getStats&urls) was announced as deprecated with the introduction of the Graph API v2.1: https://developers.facebook.com/docs/apps/changelog#v2_1_deprecations but also already in 2011: https://developers.facebook.com/blog/post/616/

So, from my understanding, this will yield in the inoperability of this feature at two years after the introduction of v2.1 on 7th August 2016:

https://developers.facebook.com/docs/apps/versions#howlong

The closest you can get to the "old" REST of FQL API call results with the Graph API is this afaik:

https://developers.facebook.com/docs/graph-api/reference/v2.3/url/

but it omits the like_count metric (don't ask me why...).

My personal opinion is that you should continue to use the FQL calls to the link_stat (https://developers.facebook.com/docs/reference/fql/link_stat/) table, because this will be available for nearly the next 2 years. This is only possible if you have a v2.0 app. Maybe there'll be some additional endpoints for the Graph API until then.

Colunga answered 24/9, 2014 at 6:58 Comment(2)
Maybe i should rise this issue on facebook developer support group, shame that not one facebook engineer has answered despite they supposedly actively participate in SO. The total value is not a problem, but it really will waste some ideas and cool use cases if there won't be a way to get likes, shares and maybe comments count separately.Kyungkyushu
As for now i'm forced to mark this as answer and give you the bounty, because there isn't any better official docs about this than what you answered, thxKyungkyushu
T
6

I suggest using a tool like SharedCount, it offers a lot more platforms too. Here is how they get the data: http://www.sharedcount.com/documentation.php (Scroll down to "Sources")

So for Facebook it would be like this:

https://api.facebook.com/method/links.getStats?urls=%%URL%%&format=json

You get a JSON object like this one:

..."share_count":66,"like_count":15,"comment_count":1,"total_count":82...

Edit: This is deprecated now, but there is another possibility, explained in this thread: Get FB likes, shares and comments for a URL using PHP - with no limit

Tomcat answered 19/9, 2014 at 20:40 Comment(9)
Not quite what i asking for, this link seems like exactly the same functionality, but is there any official documentation about it, anything?Kyungkyushu
i have edit my answer to include the result from that stats page, isn´t that exactly what you want?Tomcat
No, i know that it works, but it is undocumented, i want an official, documented graph api call.Kyungkyushu
well, i am pretty sure there is no official call, i never found that one in the graph api and i am pretty sure there is no other solution.Tomcat
So, if i want to make a top shared url section on my site i have no guarantee that some day fb just disables that possibility.Kyungkyushu
well, even with an "official solution", this would never be guaranteed ;) (see v2.0 changes)Tomcat
But they have and show that data anyway, it is used by lots of websites and yet, there are no official docs about this, it is insane. Do we at least know what is the origin of api.facebook.com/restserver.php?method=links.getStats link, how people discovered it?Kyungkyushu
no documentation is 100% accurate - and facebook changes a lot all the time, you know. and at least i don´t know where that link comes from, but it works since a very long time.Tomcat
i have edited the answer. please don´t downvote just because the information is outdated.Tomcat
V
5

You can use facebook graph api like https://graph.facebook.com/?ids=http://mycodingtricks.com and it will return a json code like

{  
    "http://mycodingtricks.com":{  
        "id":"http://mycodingtricks.com",
        "shares":1
    }
}

I have developed my own php script on which you can all social count using that api. http://mycodingtricks.com/share/social.php?url=YOUR-URL-HERE and it will return data like:

{  
    "facebook":[  
        {  
            "share_count":1,
            "like_count":0,
            "comment_count":0,
            "total_count":1,
            "click_count":0,
            "comments_fbid":567687199998199,
            "commentsbox_count":0
        }
    ],
    "googleplus":10,
    "twitter":3,
    "buffer":0,
    "pinterest":0,
    "stumblupon":1,
    "reddit":"<html><body><h1>403 Forbidden<\/h1>\nRequest forbidden by administrative rules.\n<\/body><\/html>\n",
    "linkedin":0
}

But if you wants to use on your own Here is a complete article about how to count facebook share,like and all. http://mycodingtricks.com/php/2-ways-to-count-facebook-likes-shares-and-comments-using-php/

Vernissage answered 27/3, 2015 at 20:44 Comment(3)
Don't know why this was voted down. It appears https://graph.facebook.com/?ids={URL} still works after Facebook deprecated v2.0.Blowfish
This works for me too -- up-voted ! I had to add an access token in the form APP_ID|APP_SECRET. The final URL I'm using is: https://graph.facebook.com/?ids={page_url}&access_token={APP_ID}|{APP_SECRET} which returns a JSON object that can be queried for the likes. Using JSon.Net the count is: using (WebClient wc = new WebClient()) { string json = wc.DownloadString(urlToJSONfile); Newtonsoft.Json.Linq.JObject fbData = Newtonsoft.Json.Linq.JObject.Parse(json); likes = fbData["page_url"]["likes"].ToObject<uint>(); }Fibrinolysin
Facebook Count stays ZERO in your app after facebook stopped supporting rest APIGloss
G
5

actually the correct way for getting the like number is this:

http://graph.facebook.com/v2.5/?id=YOUR_URL&fields=og_object{engagement}

the brackets after the og_object is the second level of og_object. see documentation here:
https://developers.facebook.com/docs/graph-api/reference/url

you might need to encode the url and brackets so it will look something like: https://graph.facebook.com/v2.5/?id=ENCODED_URL&fields=og_object%7Bengagement%7D&id=http%3A%2F%2Fwww.cnn.com

see a working example in graph explorer (hit the submit button)

Guttering answered 10/2, 2016 at 13:45 Comment(1)
It seems to me that this number is still the sum of shares, likes, & comments.Prismatoid
E
1

All answers are wrong or incomplete since latest shut down of API 2.1!

I am the author of the social media plugin MashShare and i recently had to do exact observations for this issue as it seriously affected the share count of our plugin.

Verified data by observations:

https://graph.facebook.com/v2.7/?id=https://www.mashshare.net contains shares and likes.

As far as i found out the only way to get a number near the real share count of a post is to get first the likes of a url. Than take this number and subtract it from the total number of shares and likes.

To get the like count you need to take the object id of the url and create another API request: https://graph.facebook.com/v2.7/14206851986099/likes?summary=true

If you know how to merge both requests into one please let me know.

Enriquetaenriquez answered 29/8, 2016 at 18:20 Comment(3)
In my testing, this separate call to get likes returns 0 in almost all cases. The original call to get share count on the URL always returns 0 for comments, so all I'm left with is one lump sum of everything. Are you getting valid numbers for likes on a URL?Prismatoid
Examples: graph.facebook.com/2.7/… Returns share_count=51,729, comment_count=0 graph.facebook.com/2.7/938331369568441/likes?summary=true Returns total_count=0 My last successful call using FQL for this same URL returned: share_count=11,985, like_count=14,213, comment_count=21,531Prismatoid
Trying this for mashshare.net or google.com in conjunction with a valid access token returns a specific number of likes. Using a full qualified link mashshare.net/downloads/mashshare-social-networks-addon is returning zero likes so yes you are right this seems not to be working as expected. I ve got the assumption that FB is planing to get rid of the like count same as twitter did recently.Enriquetaenriquez
S
0

As of August 19th 2016 facebook completely shutdown their rest API. The only alternative is to use the graph API as suggested above.

Skylark answered 22/8, 2016 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.