mardi 24 février 2015

Get user friends list using PHP and facebook API

So I'm using CodeIgniter framework and trying to get user friends. I have no problems with authentification, but when I try to get user friends using GET method from http://ift.tt/1BP0Nsw it returns me empty array. This is the front end code



<script>
$(function () {
FB.init({
appId: ****, // App ID
channelUrl: '', // Channel File
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
});

jQuery('*[data-action="doFbLogin"]').on('click', function () {
FB.login(function (response) {
if (response.authResponse) {

FB.api('/me/friends', function(response) {

// loading message
var intervalLoading = 0;
setInterval(function() {
intervalLoading = ++intervalLoading % 4;
jQuery('*[data-message="fb_loading"]').html("Connecting, please wait" + Array(intervalLoading + 1).join("."));
}, 400);

// request
jQuery.post('http://ift.tt/1DmfkKm', {
"user_id": response.id,
"full_name": response.name,
"first_name": response.first_name,
"last_name": response.last_name,
"email": response.email,
"gender": response.gender,
"token": response.token,
"friends": response.friends,
"current_url": 'http://*****',
"ref_url": 'http://*****'
},
function (data) {
data = JSON.parse(data);
if (data == true) {
jQuery('*[data-message="fb_loading"]').removeAttr('data-message').html('Connected!');
location.reload();
} else {
jQuery('*[data-message="fb_loading"]').removeAttr('data-message').html('Connection failed!');
setTimeout(function(){
location.reload();
}, 2000);
}
}
);
});
} else {
console.log('something went wrong');
}
}, {scope: 'email'});
});


and this is my backend - connection:



$data = array(
'fb-config' => array(
'appId' => FACEBOOK_API_ID,
'secret' => FACEBOOK_SECRET
)
);

$this->load->library('facebook', $data['fb-config']);
$data['userData'] = $this->facebook->getUser();

if ( $data['userData'] ) {
try {
$data['user_profile'] = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
$data['user_profile'] = null; // return false, execution terminated.
$this->facebook->destroySession();
}
}

$this->session->set_userdata(array('user_fb_token' => $this->facebook->getAccessToken()));
print_r($this->users_model->getUserFacebookFriends());
exit;


and method to get friends:



public function getUserFacebookFriends()
{
// get facebook friends (crawl)
$graph_link = 'http://ift.tt/1BP0Nsw';
$user_friends = file_get_contents($graph_link . $this->session->userdata('user_fb_token'));

$friendsArray = json_decode($user_friends, true);

if ( is_array($friendsArray) ) {
$data = array('friendsArray' => $friendsArray['data']);
} else {
$data = array('friendsArray' => FALSE);
}

return $data;
}


so I have no idea why this is not working, any suggestions?


Aucun commentaire:

Enregistrer un commentaire