Reputation: 35
I had made few FB apps on sdk v3.3 and v4.0
but now code seems to be totally changed I've used the following code to get details about the user but it hardly gives id and name not even emailID, gender, firstname and lastname.
$fb->setDefaultAccessToken($accessToken);
$_SESSION['facebook'] = $accessToken;
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
$id = $userNode->getId();
print_r($userNode);
i've even tried
$id = $userNode->getId();
$res = $fb->get('/' . $id);
$user = $res->getGraphUser();
print_r($user);
but hard luck the output was the same
Facebook\GraphNodes\GraphUser Object ( [items:protected] => Array ( [name] => xxxxxxx [id] => 11656415901xxxxx ) )
Upvotes: 1
Views: 2550
Reputation: 1663
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.4',
//'default_access_token' => '{access-token}', // optional
]);
try {
// Get the Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/me', '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();
$responseGender = $fb->get('/me?fields=gender', '{access-token}');
print_r($responseGender);
Original: https://github.com/facebook/facebook-php-sdk-v4 There last stable is v5, so I think this is the right way.
Upvotes: 2