2014-02-10 132 views
-1

我想通過使用Facebook登錄一個網站。爲此,我採取了Facebook的PHP SDK的幫助。現在我的代碼看起來像這樣facebook php sdk使登錄和註銷?

<div id="fb-root"></div> 

<script type="text/javascript"> 
//<![CDATA[ 
window.fbAsyncInit = function() { 
    FB.init({ 
    appId  : 'xxxxxxxxxxxxxx', // App ID 
    channelURL : '', // Channel File, not required so leave empty 
    status  : true, // check login status 
    cookie  : true, // enable cookies to allow the server to access the session 
    oauth  : true, // enable OAuth 2.0 
    xfbml  : false // parse XFBML 
    }); 
}; 
// logs the user in the application and facebook 
function login(){ 
FB.getLoginStatus(function(r){ 
    if(r.status === 'connected'){ 
      window.location.href = 'fbconnect.php'; 
    }else{ 
     FB.login(function(response) { 
       if(response.authResponse) { 
       //if (response.perms) 
        window.location.href = 'fbconnect.php'; 
      } else { 
       // user is not logged in 
      } 
    },{scope:'email'}); // which data to access from user profile 
} 
}); 
} 
// Load the SDK Asynchronously 
(function() { 
    var e = document.createElement('script'); e.async = true; 
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; 
    document.getElementById('fb-root').appendChild(e); 
}()); 
//]]> 
</script> 
<?php 
require_once 'src/facebook.php'; //include the facebook php sdk 
$facebook = new Facebook(array(
     'appId' => 'xxxxxxxxxxxxxx', //app id 
     'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // app secret 
)); 
$user = $facebook->getUser(); 
if ($user) { // check if current user is authenticated 
    try { 
     // Proceed knowing you have a logged in user who's authenticated. 
     $user_profile = $facebook->api('/me'); //get current user's profile information using open graph 
      } 
     catch(Exception $e){} 
} 
?> 
<a href='#' onclick='login();'>Facebook Login</a> 

現在,使用此代碼我可以輕鬆地進行登錄。但在此之後,我需要兩件事。

  • 登錄後進行註銷。那麼如何在這裏做出註銷功能 ,這樣當用戶點擊註銷時,他將一次從 臉書和網站註銷。
  • 如何獲得用戶信息登錄像他的名字,電子郵件ID等

所以有人可以請你告訴我如何做到這一點後?我只是Facebook應用程序中的新手。所以,任何幫助和建議都將是可觀的。由於

回答

0

要獲得登出網址ü可以試試這個:

$fbUserId = $facebook->getUser(); 

if ($fbUserId) { 
    $host = $_SERVER['HTTP_HOST']; 
    $params = array('next' => "http://{$host}/project/page.php"); 
    $logoutUrl = $facebook->getLogoutUrl($params); 
} 

用於獲取用戶詳細信息,你可以試試這個

try { 
    $user_profile = $facebook->api('/me'); 
} catch (FacebookApiException $e) { 
    return false; 
} 

return $user_profile; 
+0

它不工作.... – NewUser