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