尝试使用facebook api登录用户,并在php站点中返回电子邮件地址 [英] Attempting to Log user in with facebook api, and returning email address in a php site

查看:163
本文介绍了尝试使用facebook api登录用户,并在php站点中返回电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码到目前为止:

This is my code thus far:

<div id='fb-root'></div>
  <script src='http://connect.facebook.net/en_US/all.js'></script>
  <script>
     FB.init({ 
        appId:'xxxxxxxxxxxx', cookie:true, 
        status:true, xfbml:true , oauth:true, channelUrl:'http://www.federationofhumanity.com/facebookconnect/channel.html'
     });</script><fb:login-button show-faces='true' width='200' max-rows='1' perms='user_birthday,user_online_presence,user_website,email'>Login using Facebook</fb:login-button><script>
    FB.getLoginStatus(function(response) {
        if (response.authResponse) {
            document.getElementById('fb-root').innerHTML = '';

        } else {
            document.getElementById('fb-root').innerHTML = '';
        }
    });
    if ($user) 
    </script>    

(FYI,此代码通过readfile命令包含在php脚本中。)就这样,我可以将某人显示为登录,但我没有任何区别于网站上的普通用户。我的网站是基于php。我需要获得他们的主要电子邮件地址,以便网站可以使用它,并提供他们到达该网站的个性化体验。我需要以某种方式获取他们的电子邮件地址,并将其变成一个变量供php使用。请帮助?

(FYI, this code is included in php script via a readfile command.) As it is, I can show someone as "logged in", but I have nothing to differentiate them from a normal user on the website. My website is php based. I need to get their primary email address so the site can use it and provide the personalized experience they're coming to the site for. I need to somehow get their email address and turn it into a variable for the php to use. Help please?

推荐答案

用户验证您的应用程序后,Facebook可以将其返回到您设置的URL [添加参数onlogin = 'window.location =https://graph.facebook.com/oauth/authorize?client_id=XXXXX&redirect_uri=http://www.yoursite.com/facebookscript;?>& r =+ window.location。 href;']

After the user authenticates your application, Facebook can return them to a URL you set [add the paramater onlogin='window.location="https://graph.facebook.com/oauth/authorize?client_id=XXXXX&redirect_uri=http://www.yoursite.com/facebookscript; ?>&r="+window.location.href;']

此脚本可以使用PHP SDK从Facebook中提取他们的电子邮件地址,现在您有权使用它。

This script can then pull their email address from Facebook using the PHP SDK now that you have authorization to use it.

// call up the php SDK
require './facebook/facebook.php';

// now connect to facebook with our secret information
$facebook = new Facebook(array(
  'appId'  => XXXXXXX,
  'secret' => XXXXXXX,
  'cookie' => true
));

// get the facebook user id
$fb_user = $facebook->getUser();

// try to get their email address
try {
  $user_info = $facebook->api('/me'); // this is their profile basics
} catch (FacebookApiException $e) {
    echo "something went wrong: ". $e; die;
}

// print email address
echo "your email address is: ". $user_info['email']

您还可以通过JavaScript SDK使用响应.authResponse函数您当前已留空。但是您仍然需要一个脚本来处理它并注册[可能通过ajax] - 我建议让用户确认他们的电子邮件地址,因为有些人会想要切换帐户。

You can also grab the email out with the JavaScript SDK using the response.authResponse function you currently have left blank. But you still need a script to kick in to process it and register them [likely via ajax] -- I'd suggest letting the user confirm their email address as some people will want to switch accounts.

这篇关于尝试使用facebook api登录用户,并在php站点中返回电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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