How to count all of the “Likes” and “Comments” in

2019-01-20 19:43发布

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!

0条回答
登录 后发表回答