How to get friends likes via Facebook API

前端 未结 3 1375
一个人的身影
一个人的身影 2020-12-13 08:02

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
3条回答
  •  长情又很酷
    2020-12-13 08:10

    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('
    %s
    ', 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.

提交回复
热议问题