How to get the email address of a Facebook user us

2020-07-18 09:57发布

问题:

I have read through this post over and over, and yet it doesn't make sense to me.

What I am trying to do is acquire a Facebook user's email address so that I can store it in a string and compare to see if it matches the emails of existing users in my MySQL database. I only need to to do this when the user first grants access rights to the app.

In the "Authenticated Referrals" section of my app developer interface, I have set "User & Friend Permissions" to include "email".

Within the "canvas" area of my site, I have this code:

include('facebook/base_facebook.php');
include('facebook/facebook.php');
$facebook = new Facebook(array(
    'appId'  => "XXXXXXXXXXXXXXX",
    'secret' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
));
$user = $facebook->getUser();
if ($user)
{
    try {
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        $user = null;
    }
}

If I var_dup() $user, I can see the user id (though not much else) which seems to be correct, so I believe I'm connecting to the right place on Facebook.

But then there was this suggested line of code, which implies that it returns an email address, but when I echo it out to see what it looks like, it doesn't return an email address:

echo "<br/>" . $facebook->getLoginUrl(array('req_perms' => 'email'));

I kind of thought it wouldn't, as it doesn't look quite right to me, but on the other hand, I just have no idea what else to do with it or where to put it.

And then I also came across this post, which has code mixed in with the HTML in a way that baffles me even further.

Assuming that my user has granted permission for my app to see their email, isn't there a simple way of getting at it? Something an inexperienced programmer like myself can grasp?

回答1:

You are using outdated method. You can use the code below to retrieve the email.

You can find more information about Facebook Connect from : http://25labs.com/tutorial-integrate-facebook-connect-to-your-website-using-php-sdk-v-3-x-x-which-uses-graph-api/

$app_id     = "xxxxxxxxx";
$app_secret = "xxxxxxxxxx";
$site_url   = "xxxxxxxxxxxxx";

include_once "src/facebook.php";

$facebook = new Facebook(array(
    'appId'     => $app_id,
    'secret'    => $app_secret,
    ));

$user = $facebook->getUser();

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

if($user){
    $logoutUrl = $facebook->getLogoutUrl();
}else{
    $loginUrl = $facebook->getLoginUrl(array(
        'scope'         => 'email',
        'redirect_uri'  => $site_url,
        ));
}

if($user){
    Echo "Email : " . $user_profile['email'];
}


回答2:

As suggested in comments you should replace req_perms with scope since it is the name for field indicating which permissions to be granted by users.

Once email permission is granted (do you see it's asked in the Auth Dialog?) you'll be able to get email field via Graph API. You can also retrieve only required fields in results by specifying 'em in the fields parameter:

$facebook->api('/me', array('fields' => 'id,email'));

BTW, Please not that both posts you refer to are old (2010), way many things changed in the platform. Be sure to read updated documentation on Permissions and Authentication



回答3:

You can concatenate a Facebook username with the e-mail address. For example, my username is "umair.hamid.79" on Facebook. It will be "umair.hamid.79@facebook.com".