Facebook login api return user email address

Paul Ledger picture Paul Ledger · Dec 2, 2013 · Viewed 14.2k times · Source

I am trying to implement a facebook login onto my website. I can get all the relevant data I need except the email address. The data will be used to store using mysql.

I came across an interesting blog that created a step by step approach to creating a login from facebook. All of this works well but I can't return the users email address so that it can be stored in the database with their name, age, profile_pic.

This is my fb_access file: NOTE I did not write all of this myself, if there is something similar it would be appreatied.

<?php
$app_id     = "XXXXX";
$app_secret = "XXXXX";
$site_url   = "http://localhost/XXXXXX/";

try{
    include_once "src/facebook.php";
}catch(Exception $e){
    error_log($e);
}
$facebook = new Facebook(array(
    'appId'     => $app_id,
    'secret'    => $app_secret,
    ));

// Get User ID
$user = $facebook->getUser();

if($user){
    try{
        $user_profile = $facebook->api('/me');
    }catch(FacebookApiException $e){
        error_log($e);
        $user = NULL;
    }
}

if($user){
    // Get logout URL  logout add username to destroy cookie
    $logoutUrl = $facebook->getLogoutUrl(array(
   'next'=>'http://localhost/XXXXX/logout.php'));
    }else{
    // Get login URL
    $loginUrl = $facebook->getLoginUrl(array(
        'scope'         => 'read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos',
        'redirect_uri'  => $site_url."?profile_builder=".md5($user),
        ));
}

if($user){
    $queries = array(
        array('method' => 'GET', 'relative_url' => '/'.$user),
        array('method' => 'GET', 'relative_url' => '/'.$user.'/home?limit=50'),
        array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'),
        array('method' => 'GET', 'relative_url' => '/'.$user.'/photos?limit=6'),
        );
    try{
        $batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
    }catch(Exception $o){
        error_log($o);
    }
    $user_info      = json_decode($batchResponse[0]['body'], TRUE);
    $feed           = json_decode($batchResponse[1]['body'], TRUE);
    $friends_list   = json_decode($batchResponse[2]['body'], TRUE);
    $photos         = json_decode($batchResponse[3]['body'], TRUE);
    if(isset($_POST['pub'])){
        try{
            $statusUpdate = $facebook->api("/$user/feed", 'post', array(
                'message'       => 'Check out 25 labs',
                'link'          => 'http://25labs.com',
                'picture'       => 'http://25labs.com/images/25-labs-160-160.jpg',
                'name'          => '25 labs | A Technology Laboratory',
                'caption'       => '25labs.com',
                'description'   => '25 labs is a Technology blog that covers the tech stuffs happening around the globe. 25 labs publishes various tutorials and articles on web designing, Facebook API, Google API etc.',
                ));
        }catch(FacebookApiException $e){
            error_log($e);
        }
    }
    if(isset($_POST['status'])){
        try{
            $statusUpdate = $facebook->api("/$user/feed", 'post', array('message'=> $_POST['status']));
        }catch(FacebookApiException $e){
            error_log($e);
        }
    }
}
?>

Where I have come stuck is retrieving the email address:

echo $user_info['first_name'];
echo $user_info['last_name'];
// these also work
echo $user_profile['first_name'];
    echo $user_profile['last_name'];

This returns the names, there is also DOB as well, which works but

echo $user_info['email'];
//or
echo $user_profile['email']; 

doesn't seem to work it returns 'Notice: Undefined index: email' in the browser. I understand what this means but I came across an example of retrieving the email here and I presume it must work as it doesn't state any where that it doesn't. I am aware that the post was created 2 years ago but I can't see why it won't work now or is it new thing that email addresses can't be passed from facebook.

Answer

phwd picture phwd · Dec 2, 2013

You have not inserted email permissions in the scope array

$loginUrl = $facebook->getLoginUrl(array(
    'scope'         => 'read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos',
    'redirect_uri'  => $site_url."?profile_builder=".md5($user),
    ));

The scope should read

 'scope'         => 'email, read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos'

See https://developers.facebook.com/docs/facebook-login/permissions/#adding and https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/