title: Unable to retrieve email from facebook profile using PHP SDK
body: <p>I've recently got started with the Facebook SDK for PHP. When I click login with Facebook it asks me for permissions including email address, which I grant in the first step. </p>
  
  <p>I am successfully able to display user id and name. However, for email I get the following error:</p>
  
  <blockquote>
    <p>Undefined index: email in C:\wamp64\www\php-graph-sdk-5.5\src\Facebook\GraphNodes\Collection.php on line 201</p>
  </blockquote>
  
  <p>Many thanks for your help.</p>
  
  <p>facebooklogin.php:</p>
  
  <pre><code>$fb = new Facebook\Facebook([
       'app_id' =&gt; '1234', 
       'app_secret' =&gt; '987654321',
       'default_graph_version' =&gt; 'v2.2',
    ]);
  
  $helper = $fb-&gt;getRedirectLoginHelper();
  if (isset($_GET['state'])) {
      $helper-&gt;getPersistentDataHandler()-&gt;set('state', $_GET['state']);
  }
  
  $permissions = ['email']; // Optional permissions
  $loginUrl = $helper-&gt;getLoginUrl('http://localhost/callback.php', 
  $permissions);
  
  echo '&lt;a href="' . htmlspecialchars($loginUrl) . '"&gt;Log in with Facebook!&lt;/a&gt;';
  </code></pre>
  
  <p>callback.php:</p>
  
  <pre><code>$fb = new Facebook\Facebook([
       'app_id' =&gt; '1234',
       'app_secret' =&gt; '987654321',
       'default_graph_version' =&gt; 'v2.2',
  ]);
  
  $helper = $fb-&gt;getRedirectLoginHelper();
  if (isset($_GET['state'])) {
      $helper-&gt;getPersistentDataHandler()-&gt;set('state', $_GET['state']);
  }
  
  try {
      $accessToken = $helper-&gt;getAccessToken();
  } catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
      echo 'Graph returned an error: ' . $e-&gt;getMessage();
      exit;
  } catch(Facebook\Exceptions\FacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
      exit;
  }
  
  if (! isset($accessToken)) {
    if ($helper-&gt;getError()) {
      header('HTTP/1.0 401 Unauthorized');
      echo "Error: " . $helper-&gt;getError() . "\n";
      echo "Error Code: " . $helper-&gt;getErrorCode() . "\n";
      echo "Error Reason: " . $helper-&gt;getErrorReason() . "\n";
      echo "Error Description: " . $helper-&gt;getErrorDescription() . "\n";
    } else {
      header('HTTP/1.0 400 Bad Request');
      echo 'Bad request';
    }
    exit;
  }
  
  // Logged in
  // The OAuth 2.0 client handler helps us manage access tokens
  $oAuth2Client = $fb-&gt;getOAuth2Client();
  
  // Get the access token metadata from /debug_token
  $tokenMetadata = $oAuth2Client-&gt;debugToken($accessToken);
  
  // Get user’s Facebook ID
  $userId = $tokenMetadata-&gt;getField('user_id');
  
   try {
       // Returns a `Facebook\FacebookResponse` object
       $response = $fb-&gt;get('/me?fields=id,name,email', $accessToken);
   } catch(Facebook\Exceptions\FacebookResponseException $e) {
       echo 'Graph returned an error: ' . $e-&gt;getMessage();
       exit;
   } catch(Facebook\Exceptions\FacebookSDKException $e) {
       echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
       exit;
   }
  
   $user = $response-&gt;getGraphUser();
  
   $userId = $user['id'];
   $name = $user['name'];
   $email = $user['email'];
  
   echo "$userId, $email";
  </code></pre>