我正在尝试使用facebook的PHP JS sdk将facebook登录注销整合到我的网站中。我的问题是我无法从实际的Facebook网站实现独立的登录/注销选项。当我点击登录按钮时,它实际上登录了用户的Facebook和我的网站。但是,在点击我的网站的注销按钮时,它只是从我的网站注销,因为我只能销毁我的会话。如果我使用autologoutlink="true"
,那么它会在登录时显示Facebook注销按钮,但它会注销实际的Facebook。我想实现像www.9gag.com这样的东西。
我真的很感激一些帮助。
这是我的代码,我正在使用CodeIgniter,并将所有内容放在view / header.php中。
?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'app_id',
'secret' => 'secret_key',
));
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
//echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
?>
<fb:login-button scope="email" autologoutlink="true" ></fb:login-button>
JS
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
channelUrl : 'site_url',
cookie: true,
logging: true,
status: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
FB.api('/me', function(userInfo) {
//console.log(userInfo);
var options_ = {
'responsedata' : userInfo,
}
$.ajax({
type: 'POST',
url: 'user/facebooklogin',
data: options_,
dataType: "text",
success: function(msg){
console.log(msg);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
console.log(textStatus);
console.log(errorThrown);
}
});
}, {scope: 'email,user_likes'});
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(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>