I am using both Facebook PHP(4.0) and JS(2.0) SDK together. I can grab the user profile data with PHP but not the user's email. The user's email is available with the Javascript SDK, but I need it in PHP.
I was using:
$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
This works but the class GraphUser does not have a function to grab the user's email in 4.0.
Any ideas how I can grab user's email with Facebook PHP SDK version 4.0 ?
// Facebook SDK v5 for PHP
// https://developers.facebook.com/docs/php/howto/example_access_token_from_javascript/5.0.0
session_start();
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.4',
]);
$helper = $fb->getJavaScriptHelper();
$accessToken = $helper->getAccessToken();
$fb->setDefaultAccessToken((string) $accessToken);
$response = $fb->get('/me?locale=en_US&fields=name,email');
$userNode = $response->getGraphUser();
var_dump(
$userNode->getField('email'), $userNode['email']
);
GraphUser
inherits from GraphObject
so you should be able to use the generic method to grab a field:
$user_profile->getProperty("email");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With