0

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?

Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
Tester
  • 2,887
  • 10
  • 30
  • 60
  • I ask you to just have a look on http://www.fbrell.com/examples/. You will get some idea. – Wanna Coffee Jan 24 '14 at 09:45
  • Are you sure the function $facebook->api() returns array() format? try echo var_dump($user_profile) to check your output – har2vey Jan 24 '14 at 09:46
  • @har2vey Well actually when I try to echo other fields such as username, it works fine. I know it's a permissions issue with accessing email, because the email shows up fine in localhost. – Tester Jan 24 '14 at 09:49
  • So in the $user_profile output the 'email' is empty? If so, it's most likely permission issue in your call, FB requires different scope for email as I recall. – har2vey Jan 24 '14 at 09:52
  • @har2vey can you provide the code for the extended permissions? I've been looking online but it's not that easy to find examples using scope. The code I tried above is actually from an example. – Tester Jan 24 '14 at 09:54
  • in your $login_url try 'scope' => 'email' instead – har2vey Jan 24 '14 at 10:09

1 Answers1

0

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'
));
madebydavid
  • 6,457
  • 2
  • 21
  • 29