doubeng9407 2013-12-02 13:27
浏览 154
已采纳

Facebook登录api返回用户电子邮件地址

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.

  • 写回答

1条回答 默认 最新

  • doutang3077 2013-12-02 13:31
    关注

    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/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP