问题
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.
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.
回答1:
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'];
回答2:
You'll need to require an additional permission from your users in order to get their email address. This permission is aptly named email
.
Depending on your method of authentication and user login, you'll have to add this email
permission to your scope
parameter. This scope
parameter is where you add additional permissions to request from your user. You can ask for these permissions as the user installs your application but you can also request permission after the user has installed your application.
For more info on the email permission, check out this link to the relevant documentation.
来源:https://stackoverflow.com/questions/16262252/how-to-get-email-from-facebook