In Facebook Graph API, what are the API calls to get a user's email address and gender?

☆樱花仙子☆ 提交于 2019-12-02 19:33:01

问题


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.


回答1:


Have a look at

  • https://developers.facebook.com/docs/graph-api/reference/v2.2/user#fields
  • https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#fields

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


来源:https://stackoverflow.com/questions/27220783/in-facebook-graph-api-what-are-the-api-calls-to-get-a-users-email-address-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!