Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get friends likes via Facebook API

Is this possible to get all my friends likes?? Now, I'm getting this by 2 steps:

  1. Getting all my friends list
  2. By iterating those list I'm getting all individuals Likes,

but it seems very big process, anybody have idea to improve the performance for this task?

like image 967
maxgrinev Avatar asked Jan 24 '11 09:01

maxgrinev


People also ask

How to like a Facebook page using API?

A timestamp indicating when the page was liked by this person. You can't publish using this edge, as it is not possible to like a Facebook Page via any API. You should use the Like Button if you want people to be able to like a page in your app.

How to get a list of our friends using API?

If we are logged in, the script will evoke API method /me/friends in order to get a list of our friends. As you can see – we use limits (99 by default) to avoid very long listings.

What is Facebook Graph API and how do I use it?

You’re going to have to approve the access of the app to your page. The main product is the Graph API, that serves as a base for all other kinds of Facebook APIs, They are in essence extensions of Graph API, and all of the Facebook products interact with it in some way.

Where can I find the full details of the Facebook API?

The full details can be found in the Pages API Documentation. Don’t forget you need a token from an app user who owns or can perform a required task on the page. There are two types provided by Facebook – Video API and Live Video API.


2 Answers

At last I found it after 2 weeks looking at Facebook API documentation

FB.api("/likes?ids=533856945,841978743")

FB.api("me/friends",{
  fields:'id',
  limit:10
},function(res){
  var l=''
  $.each(res.data,function(idx,val){
     l=l+val.id+(idx<res.data.length-1?',':'')
  })
  FB.api("likes?ids="+l,function(res){
      console.log(res);
  })
})
like image 64
dupdup Avatar answered Jan 22 '23 04:01

dupdup


Using FQL is going to be faster than looping through the Graph API results. You can get the ID of the pages your friends like, but unfortunately FQL does not return info other than that (ie the name). Take a look at the following.

This assumes you are using the PHP SDK with the friends_likes permission.

// hold on to your user ID
$user_id = $facebook->getUser();

// query your friend's likes based on their ID
$query = "SELECT uid, page_id FROM page_fan WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = $user_id)";
$result = $fb->api(array(
  'method' => 'fql.query',
  'query' => $query,
));

// optionally group the results by each friend ID
function arraySort($input, $sortkey){
  foreach ($input as $key => $val) {
    $output[ $val [ $sortkey ] ][] = $val;
  }
  return $output;
}
$friendLikes = arraySort($result,'uid');

// output the results
echo sprintf('<pre>%s</pre>', print_r($friendLikes,TRUE));

The benefit of this is that you only make one API call. You will have to make separate calls to get the friend names and another for the liked page details, but you have the IDs to work with now in a straight forward approach.

like image 34
Carson Avatar answered Jan 22 '23 05:01

Carson