facebook php sdk进行登录和注销?

时间:2014-02-10 11:04:02

标签: php facebook facebook-php-sdk

我正在尝试使用Facebook登录网站。为此,我已经采取了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>

现在使用此代码我可以轻松登录。但在此之后,我还需要两件事。

  • 登录后注销。那么如何在这里进行注销功能呢 这样,当用户点击退出时,他将从两者注销 Facebook和网站一次。
  • 如何在登录后获取用户信息,如姓名,电子邮件ID等。

有人可以告诉我该怎么做吗?我只是facebook应用程序中的新手。因此,任何帮助和建议都会令人感到震惊。感谢

1 个答案:

答案 0 :(得分: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;