在Facebook Graph API中,获取用户电子邮件地址和性别的API调用是什么?

在Facebook Graph API中,获取用户电子邮件地址和性别的API调用是什么?

问题描述:

I'm using the Graph API and the Facebook SDK for PHP with the help of this link: https://developers.facebook.com/docs/php/howto/profilewithgraphapi/4.0.0. This is the code:

use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

if($session) {

  try {

    $user_profile = (new FacebookRequest(
      $session, 'GET', '/me'
    ))->execute()->getGraphObject(GraphUser::className());

    echo "Name: " . $user_profile->getName();

  } catch(FacebookRequestException $e) {

    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();

  }   

}

I'm at the point where I'm able to get a user's name by using user_profile->getName();, I can also get his/her first name, last name, middle name, etc., according to the API calls here: https://developers.facebook.com/docs/php/GraphObject/#user-instance-methods. I know it is possible to get a user's email address and gender, but in the previous link I didn't find any API calls to be able to get this information.

For Facebook SDK for PHP, what are the API calls to get a user's email address and gender?

Thank you very much.

我借助此链接使用Graph API和Facebook SDK for PHP: https://developers.facebook.com/docs/php/howto/profilewithgraphapi/4.0.0 。 这是代码: p>

 使用Facebook \ FacebookRequest; 
use Facebook \ GraphUser; 
use Facebook \ FacebookRequestException; 
 
if($ session){
 
尝试 {
 
 $ user_profile =(新的FacebookRequest(
 $ session,'GET','/ me'
)) - > execute() - > getGraphObject(GraphUser :: className()); 
  
 echo“姓名:”。  $ user_profile-> getName(); 
 
}} catch(FacebookRequestException $ e){
 
 echo“异常发生,代码:”。  $ e-> getCode(); 
 echo“with message:”。  $ e-> getMessage(); 
 
} 
 
} 
   code>  pre> 
 
 

我正处于能够获得 用户的名字通过 user_profile-> getName(); code>,我也可以根据这里的API调用得到他/她的名字,姓氏,中间名等: https://developers.facebook.com/docs/php/GraphObject/#user-instance - 方法。 我知道可以获取用户的电子邮件地址和性别,但在上一个链接中,我没有找到任何API调用来获取此信息。 p>

对于Facebook SDK for PHP,获取用户电子邮件地址和性别的API调用是什么? p>

非常感谢。 p> div>

Have a look at

to get an overview what fields and edges you can query for the user object. In your case, that should be

/me?fields=id,gender,email

or

new FacebookRequest(
  $session, 'GET', '/me?fields=id,gender,email'
)

Be aware that you'll need the user_email permission to be able to request the email field.

To get specific fields, use the getProperty() method as described at https://developers.facebook.com/docs/php/GraphObject/#getproperty

echo $user_profile->getProperty('gender');