dickyj
dickyj

Reputation: 1870

How to query the like count of a facebook object such as a album or photo via fql?

Here is a tricky question, how do you get the like count of a facebook object such as an album or a photo? I know that facebook has a fql table called link_stat that allow you to get the like count for an external url, but it does not seem to work if the object is within facebook. If you access the likes via the opengraph api on a particular object, it will only return a maximum of 4 users that like the object, even though there maybe a few thousand users that like it. Any clues?

Upvotes: 4

Views: 8526

Answers (5)

Loïc Joachim
Loïc Joachim

Reputation: 109

Obviously the answer has changed a lot since you first asked the question but here is the answer as of today. Use the Facebook Graph API to figure out how to get the info about each photo in the album from Facebook:

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

Use 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;
}

Post the result into your HTML using ID's:

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

Working demo here.

Hope this helps!

Upvotes: 1

Pete
Pete

Reputation: 45

These days, if you know what type of post you are querying, you may be able to use FQL. For example if you want to know how many likes there are for a photo you can use:

SELECT like_info FROM photo WHERE object_id = 414122065292107

Which returns the following:

{
  "data": [
    {
      "like_info": {
        "can_like": true, 
        "like_count": 1354, 
        "user_likes": false
      }
    }
  ]
}

Similar approaches exist for different types of content but it seems inconsistent to me so have a look in the FQL documentation.

https://developers.facebook.com/docs/reference/fql/

Upvotes: 0

van thanh
van thanh

Reputation: 11

# fql query
   $fql = "SELECT like_info FROM photo WHERE object_id=" . $photo_id;

   # api request
   $request = array(
    'method' => 'fql.query', 'query' => $fql
   );

  # run batch request
  $likeInfo = $facebook->api(array(
    'method' => 'fql.query', 'query' => $fql
  ));

  var_dump($likeInfo);die;
=> get like info of photo`enter code here`

Upvotes: 1

Haroon
Haroon

Reputation: 26

I was able to do this by changing privacy settings. If you query the tables for the likes of XYZ user it will not return the data until and unless the user has set the Visibility to friends (for photo, album, video, link or a note) no matter if you have asked for the permissions( from within your application).

So for instance I am XYZ user and i liked a certain link posted by my GHI. and i use the application developed for Facebook and Allow access to my links. now if the application tries to access links that i have liked on Facebook the application will not be able to get the link posted by GHI (that i liked) until and unless the GHI has set the visibility of his her links to Friends.

if any one needs more help on this then get back on this forum.

regards, Haroon

Upvotes: 1

Bot
Bot

Reputation: 11855

See http://developers.facebook.com/docs/reference/fql/like

You can do fql query to retrieve a list of the likes based on the object_id then do a count.

Upvotes: 1

Related Questions