How to count all of the “Likes” and “Comments” in a Facebook photo album?

感情迁移 提交于 2019-12-17 23:00:30

问题


Just thought I would share the answer to the problem I had. I was looking for a way to count all the likes and comments from each of the photos in my photo album on Facebook. My photo album had thousands of likes and comments spread out over hundreds of photos so there was no way it could be done by hand. I couldn't find an existing way to do it automatically so here is my solution.

After lots of experimenting with the Facebook Graph API trying to figure out how to get the info from Facebook this is the final working URL:

https://graph.facebook.com/albumID/photos?fields=id,likes.summary(true),comments.summary(true)&after=XXXXXX&access_token=XXXXXX

Used Ajax to send the GET request:

$.ajax({
    dataType: "json",
    method: "GET",
    url: "https://graph.facebook.com/" + albumID + "/photos",
    data: {fields: "id,likes.summary(true),comments.summary(true)",
        limit: 100,
        after: afterStr,
        access_token: token})

The variable 'afterStr' is the ID of the next page of data.

Then the following to count the likes and comments we got from facebook:

var dArr = msg.data;
var i = 0;
for (i = 0; i < dArr.length; i++) {
    like += dArr[i].likes.summary.total_count;
    comment += dArr[i].comments.summary.total_count;
}

Then post the result into your HTML using ID's:

$("#likeID").html(like);
$("#commentID").html(comment);

Working demo here: http://scholatec.com/article/facebook-counter

Hope this helps someone!

来源:https://stackoverflow.com/questions/32085722/how-to-count-all-of-the-likes-and-comments-in-a-facebook-photo-album

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!