Facebook Graph API(如何获得用户性别?)

时间:2015-09-04 09:21:36

标签: php facebook facebook-graph-api

我正在研究PHP项目。我必须得到用户的id,姓名,名字,性别和电子邮件。我得到了用户的身份。所以,很明显PHP facebook-sdk或Facebook库中没有任何问题。但我无法得到其他领域。所以,我的代码中出了问题。但我找不到错误的代码。如果有人知道答案,请解释或建议我从一开始就能理解的链接。谢谢。这是我的代码。这是我的代码。

$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();

// get response
$graphObject = $response->getGraphObject();

$fbid = $graphObject->getProperty('id');         // To Get Facebook ID

echo $fbid;    //working as I'm getting facebook ID

$filenameIn = 'https://graph.facebook.com/' . $fbid . '/fields=gender';
$gender = file_get_contents($filenameIn);
echo $gender;    //I can not get this.

1 个答案:

答案 0 :(得分:4)

CBroe是对的。我犯了那个错误。当我改变我的代码时,正如CBroe建议的那样,我遇到了问题。这是正确的代码。

$request = new FacebookRequest($session, 'GET', '/me?fields=id,gender');
$response = $request->execute();

// get response
$graphObject = $response->getGraphObject();

$fbid = $graphObject->getProperty('id');         // To Get Facebook ID
$fbgender = $graphObject->getProperty('gender'); // To Get Facebook gender

echo $fbid;    //working as I'm getting facebook ID
echo $fbgender;//working as I'm getting facebook gender