Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook PHP SDK how to get user email from Facebook JavaScript SDK 2014

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 ?

like image 976
user3753569 Avatar asked Nov 26 '22 23:11

user3753569


2 Answers

// 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']
);
like image 86
masakielastic Avatar answered Dec 06 '22 00:12

masakielastic


GraphUser inherits from GraphObject so you should be able to use the generic method to grab a field:

$user_profile->getProperty("email");
like image 32
Fosco Avatar answered Dec 05 '22 23:12

Fosco