开发者

How do you get the email using facebook api?

I'm using this code but I can't figure out how to get the email from the user. I know you must get special permission from the user but I don't know how.

I can get the public data from the u开发者_开发百科ser but that's not what I even need. The only thing I need is the email.

I guess it's something with this line:

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

I have read the documentation but I still don't know how I can get the email. How do I get the email from the user loggin in on my website with the facebook api?


You first need to request an extended permission "Email" from the user. You can do this using FB JS SDK by calling FB.login(), or PHP FB SDK by redirecting the browser by calling getLoginUrl().

FB JS SDK: FB.login method

FB.login(function(response) {
  if (response.session) {
    if (response.perms) {
      // user is logged in and granted some permissions.
      // perms is a comma separated list of granted permissions
    } else {
      // user is logged in, but did not grant any permissions
    }
  } else {
    // user is not logged in
  }
}, {perms:'email'});

FB PHP SDK method:

$login_url = $Facebook->getLoginUrl(array(
    'canvas'    => 1,
    'fbconnect' => 0,
    'req_perms' => 'email'
));

echo '<script type="text/javascript">top.location.href = "'.$login_url.'";</script>';
exit();

Once you have the required permission, you can then call this in your PHP:

$me = $facebook->api('/me?fields=email');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜