从Facebook令牌元数据响应中获取'user_id'值

Red*_*rus 1 php facebook

我正在使用以下Facebook SDK登录我的网站Facebook.我在Facebook Developers上找到了源代码.

$fb = new Facebook\Facebook([
    'app_id' => $fbapp_id,
    'app_secret' => $fbapp_secret,
    'default_graph_version' => 'v2.4',
]);

$helper = $fb->getRedirectLoginHelper();

try {
  $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  $msg = 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  $msg = 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

if ( !isset($accessToken) ) {
  if ($helper->getError()) {
    header('HTTP/1.0 401 Unauthorized');
    echo "Error: " . $helper->getError() . "\n";
    echo "Error Code: " . $helper->getErrorCode() . "\n";
    echo "Error Reason: " . $helper->getErrorReason() . "\n";
    echo "Error Description: " . $helper->getErrorDescription() . "\n";
  } else {
    header('HTTP/1.0 400 Bad Request');
    $msg = 'Bad request';
  }
  exit;
}

// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();

// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
var_dump($tokenMetadata);

$_SESSION['fb_access_token'] = (string) $accessToken;
Run Code Online (Sandbox Code Playgroud)

通过使用上面的代码,我得到以下响应.如何从此数组中获取'user_id'值.

string(208) "CAAQJ73gQW1kBAEeG6aOH5aPZBmULTxJPuJ8qOliC1Xn5ljjBYVHHuXuiKAn04Dz2D6hdcZBiHqhtLhe8oR1b3M78KxxUsKOj3QzQsPEnPTqxw54MK026ljHxz6EbACMdgNYgQ0jO6x6x5YGdkdIari6Nhya8ea68gTpHArl8MxexXnkZCpBXOQxZAXQ4y80YNVZCsR9X89QZDZD"
object(Facebook\Authentication\AccessTokenMetadata)#13 (1) {
  ["metadata":protected]=>
  array(7) {
    ["app_id"]=>
    string(16) "1136824023014233"
    ["application"]=>
    string(8) "example.com"
    ["expires_at"]=>
    object(DateTime)#17 (3) {
      ["date"]=>
      string(26) "2015-11-18 06:36:01.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }
    ["is_valid"]=>
    bool(true)
    ["issued_at"]=>
    object(DateTime)#18 (3) {
      ["date"]=>
      string(26) "2015-09-19 06:36:01.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }
    ["scopes"]=>
    array(2) {
      [0]=>
      string(5) "email"
      [1]=>
      string(14) "public_profile"
    }
    ["user_id"]=>
    string(17) "1020772222323227"
  }
}
Run Code Online (Sandbox Code Playgroud)

Red*_*rus 6

我已设法User Id使用以下内容

$getUserId = $tokenMetadata->getUserId();
Run Code Online (Sandbox Code Playgroud)