0

I'm using code from here for login with facebook and this is my first play with Oauth and facebook graph api. Fetching logged in user's details is working fine but when I added code for fetching friend list I got empty array.

<?php
include_once("config.php");
include_once("includes/functions.php");

$friends_list = '';
if(!$fbuser){
 $fbuser = null;
 $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
 $output = '<a href="'.$loginUrl.'"><img src="images/fb_login.png"></a>';  
}else{
 $user_profile = $facebook->api('/me?fields=id,first_name,last_name,email,gender,locale,picture');
 $friends = $facebook->api('/me/friends','GET');//<-- this line is not working
 $user = new Users();
 $user_data = $user->checkUser('facebook',$user_profile['id'],$user_profile['first_name'],$user_profile['last_name'],$user_profile['email'],$user_profile['gender'],$user_profile['locale'],$user_profile['picture']['data']['url']);
 if(!empty($user_data)){
  $output = '<h1>Facebook Profile Details </h1>';
  $output .= '<img src="'.$user_data['picture'].'">';
                $output .= '<br/>Facebook ID : ' . $user_data['oauth_uid'];
                $output .= '<br/>Name : ' . $user_data['fname'].' '.$user_data['lname'];
                $output .= '<br/>Email : ' . $user_data['email'];
                $output .= '<br/>Gender : ' . $user_data['gender'];
                $output .= '<br/>Locale : ' . $user_data['locale'];
                $output .= '<br/>You are login with : Facebook';
                $output .= '<br/>Logout from <a href="logout.php?logout">Facebook</a>'; 
  $friends_list = $friends;
 }else{
  $output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
 }
}
?>
<!DOCTYPE html>
<html lang = "en">
<body>
<div class="container">
 <div class="col-md-12">
  <?php if($output){
    echo $output."</br>";
    print_r($friends_list);
   } 
  ?><br>
 </div>
</div>
</body>
</html>

I'm getting print_r($friends_list); output like Array ( [data] => Array ( ) ).

My facebook API version is v2.6 and I'm running my script on localhost. So why I'm getting an empty array of friend list, please suggest any changes.

Vilas
  • 837
  • 2
  • 15
  • 41

1 Answers1

2

An empty array is the correct result. You can only get friends who authorized your App too (with the user_friends permission). Since this is a new App, i assume that none of your friends authorized your App yet. Although, you should get the "total_count" in the response.

More information: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app

Community
  • 1
  • 1
andyrandy
  • 72,880
  • 8
  • 113
  • 130