简体   繁体   English

如何使用PHP SDK V5获取用户性别

[英]how to get user gender using php sdk v5

I had made few FB apps on sdk v3.3 and v4.0 我在sdk v3.3和v4.0上制作了一些FB应用程序

but now code seems to be totally changed I've used the following code to get details about the user but it hardly gives id and name not even emailID, gender, firstname and lastname. 但是现在代码似乎已完全更改,我已经使用以下代码来获取有关用户的详细信息,但几乎没有给出id和名称,甚至没有提供emailID,性别,名字和姓氏。

  $fb->setDefaultAccessToken($accessToken);
  $_SESSION['facebook'] = $accessToken;
  $response = $fb->get('/me');

  $userNode = $response->getGraphUser();
  $id = $userNode->getId();

  print_r($userNode);

i've even tried 我什至尝试过

$id = $userNode->getId();
$res = $fb->get('/' . $id);

$user = $res->getGraphUser();
print_r($user);

but hard luck the output was the same 但幸运的是输出是相同的

Facebook\\GraphNodes\\GraphUser Object ( [items:protected] => Array ( [name] => xxxxxxx [id] => 11656415901xxxxx ) ) Facebook \\ GraphNodes \\ GraphUser对象([items:protected] =>数组([name] => xxxxxxx [id] => 11656415901xxxxx))

$fb = new Facebook\Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v2.4',
  //'default_access_token' => '{access-token}', // optional
]);

try {
  // Get the Facebook\GraphNodes\GraphUser object for the current user.
  // If you provided a 'default_access_token', the '{access-token}' is optional.
  $response = $fb->get('/me', '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();
$responseGender = $fb->get('/me?fields=gender', '{access-token}');
print_r($responseGender);

Original: https://github.com/facebook/facebook-php-sdk-v4 There last stable is v5, so I think this is the right way. 原文: https : //github.com/facebook/facebook-php-sdk-v4最后一个稳定版是v5,所以我认为这是正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM