Domanda

I am trying to access the user's email address using Facebook php SDK v 3.2.3

The code:

<?php


 require_once('facebook-php-sdk/src/facebook.php');
// Create our Application instance (replace this with your appId and secret).
$facebook= new Facebook(array(
  'appId' => 'xxxxxxx',
      'secret' => 'xxxxxxxxxxxxxxx',
      'allowSignedRequest' => false,
));

$_REQUEST += $_GET;
$user = $facebook->getUser();

if ($user) {
  try {

 $login_url = $facebook->getLoginUrl(array(
    'req_perms' => 'email'
  ));
  echo 'Please <a href="' . $login_url . '">login.</a>';

    $user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
    echo $user_profile['email'];
    echo $user_profile['username'];

  } catch (FacebookApiException $e) {
    $user = null;
  }
} else {
  die('<script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}

?>

I tried the above but it didn't work. I tried getting the extending permissions via the login but no luck, not sure if I did this right.

I get the error "Undefined index: email".

How do I fix this?

È stato utile?

Soluzione

To specify the permissions required, you need to pass them in the array passed to the getLoginUrl function with a key called scope.

$login_url = $facebook->getLoginUrl(array(
    'scope' => 'email'
));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top