如何从Facebook获取电子邮件 [英] How to get email from facebook

查看:244
本文介绍了如何从Facebook获取电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个应用程序.有一种形式,其中有两个字段,电子邮件和姓名.有一个按钮,使您可以登录facebook,然后它将获得用户名和电子邮件.

I have created an application. There is form, in which there are two fields email and name. There is button, which enable you to login with facebook and after that it will get the user name and email.

我尝试了很多方法.我可以获取名称和其他详细信息,但无法获取用户电子邮件.我还使用了fql来获取电子邮件,但没有得到.下面是代码.我想用PHP检索它.

I have tried many ways. I can get name and other detail, but I couldn't get user email. I have also used fql to get the email but didn't get. Below is the code. I want to retrive that by PHP.

$fql = 'SELECT contact_email FROM user WHERE uid = '.$user;
$res = $facebook->api(array('method' => 'fql.query',
                            'query' => $fql));

任何人都可以告诉我如何获取电子邮件.任何帮助都将是可贵的.

Can any one will tell me how to get the email. Any help will be appreciable.

推荐答案

在电子邮件权限中使用facebook->getLoginUrl,请参阅

use facebook->getLoginUrl with Email Permissions, see https://developers.facebook.com/docs/reference/login/email-permissions/ (scope' => 'email')

facebook.php :

<?
error_reporting(E_ALL);
ini_set('display_errors','on');
require 'facebook-php-sdk-master/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => <YOUR AppId>,
  'secret' => <YOUR APPsecret>,
));

$applicationurl = 'http://testdrive.nl/facebook.php';

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

if(empty($user))
{
    $params = array(
  'scope' => 'email',
  'redirect_uri' => $applicationurl
   );   

$loginUrl = $facebook->getLoginUrl($params);
header('Location: ' . $loginUrl ."\r\n");
exit;
}   

$fql = 'SELECT contact_email FROM user WHERE uid = '.$user;
$res = $facebook->api(array('method' => 'fql.query',
                            'query' => $fql));
//var_dump($res);
echo $res[0]['contact_email'];

这篇关于如何从Facebook获取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆