I am coding an app that uses the Facebook API. In my previous apps, retrieving a user's friend list was easy (some array with all the friends' ids was returned).
With the current API, I cannot found a way to retrieve the very same list. I have read everything that's available on the topic (both S/O and Facebook Dev Docs) but I don't have the final answer: is such a thing still possible.
Here's my code.
<?php
require 'src/facebook.php'; // Include facebook SDK file
//require 'functions.php'; // Include functions
$facebook = new Facebook(array(
'appId' => 'xxxx', // Facebook App ID
'secret' => 'xxxx', // Facebook App Secret
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$user_friends = $facebook->api('/me/friends');
$fbid = $user_profile['id']; // To Get Facebook ID
$fbuname = $user_profile['username']; // To Get Facebook Username
$fbfullname = $user_profile['name']; // To Get Facebook full name
$femail = $user_profile['email']; // To Get Facebook email ID
$friends=implode($user_friends,',');
$link = mysql_connect("localhost", "xxx", "xxx")
or die("Impossible de se connecter : " . mysql_error());
mysql_select_db('mirror',$link);
$sql = "SELECT * FROM users WHERE email = '".$femail."' ";
// on envoie la requête
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
$num_rows = mysql_num_rows($req);
if($num_rows==0){
$sql = "INSERT INTO users (email, name, fbid,friends) VALUES ('".$femail."','".utf8_decode($fbfullname)."','".$fbid."','".$friends."') ";
// on envoie la requête
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
}
if($num_rows!=0){
$sql = "UPDATE users SET fbid = '".$fbid."',friends='".$friends."' ";
// on envoie la requête
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
}
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['USERNAME'] = $fbuname;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
// checkuser($fbid,$fbuname,$fbfullname,$femail); // To update local DB
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
header("Location: index.php");
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email , user_likes, read_friendlists'
));
header("Location: ".$loginUrl);
}
anything related to friends is either an empty array or an empty var.