如何使用php sdk v5获取用户性别

时间:2015-08-14 12:22:49

标签: php facebook

我在sdk v3.3和v4.0

上制作了几个FB应用程序

但现在代码似乎完全改变我已经使用以下代码来获取有关用户的详细信息,但它几乎不提供id和名称甚至不是emailID,性别,名字和姓氏。

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

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

  print_r($userNode);

我甚至尝试过

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

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

但运气不好输出

  

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

1 个答案:

答案 0 :(得分:2)

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

原文:https://github.com/facebook/facebook-php-sdk-v4 最后一个稳定版是v5,所以我认为这是正确的方法。