2011-10-07 18 views
0

這是我的代碼至今:試圖與Facebook的API來登錄用戶,並在一個PHP的網站返回的電子郵件地址

<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>  

(僅供參考,該代碼是通過ReadFile的命令包含在PHP腳本)。事實上,我可以將某人顯示爲「已登錄」,但我沒有任何區別他們與網站上的普通用戶。我的網站是基於PHP的。我需要獲取他們的主要電子郵件地址,以便網站可以使用它並提供他們訪問網站的個性化體驗。我需要以某種方式獲得他們的電子郵件地址,並將其變成一個變量供PHP使用。請幫助?

回答

1

在用戶驗證您的應用程序後,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;']

此腳本可以使用PHP SDK將他們的電子郵件地址從Facebook上取下,有權使用它。

// 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'] 

您也可以使用您目前已留空response.authResponse功能搶出電子郵件使用JavaScript SDK。但你仍然需要一個腳本來處理它並註冊它們(可能通過ajax) - 我建議讓用戶確認他們的電子郵件地址,因爲有些人會想要切換帳戶。

相關問題