Facebook PHP SDK 如何从 Facebook JavaScript SDK 2014 获取用户电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24662427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:11:31  来源:igfitidea点击:

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

javascriptphpfacebookfacebook-graph-apisdk

提问by user3753569

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.

我同时使用 Facebook PHP(4.0) 和 JS(2.0) SDK。我可以使用 PHP 获取用户配置文件数据,但不能获取用户的电子邮件。用户的电子邮件可通过 Javascript SDK 获得,但我需要在 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.

这有效,但 GraphUser 类在 4.0 中没有获取用户电子邮件的功能。

Any ideas how I can grab user's email with Facebook PHP SDK version 4.0 ?

任何想法如何使用 Facebook PHP SDK 4.0 版获取用户的电子邮件?

回答by masakielastic

// 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']
);

回答by Fosco

GraphUserinherits from GraphObjectso you should be able to use the generic method to grab a field:

GraphUser继承自,GraphObject因此您应该能够使用泛型方法来获取字段:

$user_profile->getProperty("email");

回答by Pardhu

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.

在获取登录 url 之前请求权限。

check permissions with facebook login here

在此处使用 facebook 登录检查权限

So you can get user email

所以你可以得到用户的电子邮件

 $user_profile['email'];

and username by

和用户名

$user_profile['username'];

回答by shyam sasi

Change FacebookRequest() Parameter as below. change getLoginUrl() parameter as below

更改 FacebookRequest() 参数如下。更改 getLoginUrl() 参数如下

   $request = new FacebookRequest( $session, 'GET', '/me?locale=en_US&fields=name,email' );
 $loginUrl = $helper->getLoginUrl( array(
    'scope' => 'email'
  ));