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
继承自GraphObject
因此您应该能够使用泛型方法来获取字段:
$user_profile->getProperty("email");
Change login url to
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email'
));
you can also add additional permissions like
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email, user_activities '
));
Request permissions before you get login url.
check permissions with facebook login here
So you can get user email
$user_profile['email'];
and username by
$user_profile['username'];
Change FacebookRequest() Parameter as below. change getLoginUrl() parameter as below
$request = new FacebookRequest( $session, 'GET', '/me?locale=en_US&fields=name,email' );
$loginUrl = $helper->getLoginUrl( array(
'scope' => 'email'
));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.