I' am using the following Facebook SDK to Login with Facebook for my Website. I found the source code on Facebook Developers.
$fb = new Facebook\Facebook([
'app_id' => $fbapp_id,
'app_secret' => $fbapp_secret,
'default_graph_version' => 'v2.4',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$msg = 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$msg = 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if ( !isset($accessToken) ) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "
";
echo "Error Code: " . $helper->getErrorCode() . "
";
echo "Error Reason: " . $helper->getErrorReason() . "
";
echo "Error Description: " . $helper->getErrorDescription() . "
";
} else {
header('HTTP/1.0 400 Bad Request');
$msg = 'Bad request';
}
exit;
}
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
var_dump($tokenMetadata);
$_SESSION['fb_access_token'] = (string) $accessToken;
By using the above code, I get the following response. How can I get the get the 'user_id' value from this array.
string(208) "CAAQJ73gQW1kBAEeG6aOH5aPZBmULTxJPuJ8qOliC1Xn5ljjBYVHHuXuiKAn04Dz2D6hdcZBiHqhtLhe8oR1b3M78KxxUsKOj3QzQsPEnPTqxw54MK026ljHxz6EbACMdgNYgQ0jO6x6x5YGdkdIari6Nhya8ea68gTpHArl8MxexXnkZCpBXOQxZAXQ4y80YNVZCsR9X89QZDZD"
object(Facebook\Authentication\AccessTokenMetadata)#13 (1) {
["metadata":protected]=>
array(7) {
["app_id"]=>
string(16) "1136824023014233"
["application"]=>
string(8) "example.com"
["expires_at"]=>
object(DateTime)#17 (3) {
["date"]=>
string(26) "2015-11-18 06:36:01.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["is_valid"]=>
bool(true)
["issued_at"]=>
object(DateTime)#18 (3) {
["date"]=>
string(26) "2015-09-19 06:36:01.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["scopes"]=>
array(2) {
[0]=>
string(5) "email"
[1]=>
string(14) "public_profile"
}
["user_id"]=>
string(17) "1020772222323227"
}
}