2014-02-10 138 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