电子邮件不是保存在数据库从Facebook id在php


email is not saved in database from Facebook id in php

我的电子邮件id没有存储在数据库中。我想在我的数据库中存储Facebook用户的详细信息。实际上,我有登录页面,用户也可以登录Facebook,但问题是,我得到所有的细节,除了电子邮件。我需要存储的电子邮件id也在数据库中。请帮帮我。任何帮助都是感激的。这是我的代码…

<?php
require 'facebook/facebook.php';
require 'config/fbconfig.php';
require 'config/functions.php';
$facebook = new Facebook(array(
    'appId' => APP_ID,
    'secret' => APP_SECRET,
));
$user = $facebook->getUser();
if ($user) {
    try {       
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
    }catch (FacebookApiException $e){
        error_log($e);
        $user = null;
    }
    if(!empty($user_profile )){
        # User info ok? Let's print it (Here we will be adding the login and registering routines)
        $username = $user_profile['name'];
        $uid = $user_profile['id'];
        echo $email = $user_profile['email'];
        $user = new User();
        $userdata = $user->checkUser($uid, 'facebook', $username,$email,$twitter_otoken,$twitter_otoken_secret);
        if(!empty($userdata)){
            session_start();
            $_SESSION['id'] = $userdata['id'];
            $_SESSION['oauth_id'] = $uid;
            $_SESSION['username'] = $userdata['username'];
            $_SESSION['email'] = $email;
            $_SESSION['oauth_provider'] = $userdata['oauth_provider'];
            header("Location: home.php");
        }
    }else{
        # For testing purposes, if there was an error, let's kill the script
        die("There was an error.");
    }
}else{
    # There's no active session, let's generate one
    $login_url = $facebook->getLoginUrl(array( 'scope' => 'email'));
    header("Location: " . $login_url);
}

这只是一个建议。

有时如果用户将不允许访问他的电子邮件id在Facebook。此时,您将无法获得该用户的电子邮件id。

在这种情况下,您可以将用户的电子邮件存储为'userid@facebook.com'。

userid在facebook中对于每个用户也是唯一的。因此,您将获得每个用户的唯一电子邮件id。

替换下面一行:

$user_profile = $facebook->api('/me');

,代码如下:

$user_profile = $facebook->api('/me?fields=id,name,first_name,last_name,gender,locale,timezone,email');