当用户单击登录按钮时,我正在使用 Facebook 的 JavaScript SDK 来弹出登录弹出窗口。
正如 Facebook 在文档中提供的那样,代码是:
$(".loginButton").click(function(){
FB.login(function(response) {
FB.api('/me', function(response) {
console.log(response.id);
//User ID shows up so I can see that the user has accepted the app.
});
});
我还可以FB.getLoginStatus()
用来检查用户是否确实已登录并接受了应用程序。
但是,现在我想用 PHP 执行一个函数。据我了解,PHP$user
在成功登录后分配了 UserID。
问题是在 JS 登录之后$user
仍然是0
. 我被卡住了,我无法弄清楚为什么它不会将正确的用户 ID 分配给$user
这是我的 JS:
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $AppId; ?>',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true,
channelUrl : '<?php echo $ServerPath; ?>channel.php' // custom channel
});
};
//Get Login Status
function amILoggedIn(){
var toreturn = "";
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
window.userId = response.authResponse.userID;
toreturn = "GetLoginStatus: logged in" + " " + "<?php echo $user; ?>";
} else {
toreturn = "GetLoginStatus: Logged Out";
}
console.log(toreturn);
});
};
// Load the SDK asynchronously
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
var obj = {
method: 'feed',
link: 'http://www.google.com'
};
$(".loginPopupButton").click(function(){
FB.login(function(response) {
FB.api('/me', function(response) { //user-specific stuff
console.log(response.id);
});
});
});
</script>
这是PHP:
<?php
include_once('fbapi/facebook.php');
include_once('fbapi/Authenticated_User.php');
include_once('config.php');
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $AppId,
'secret' => $AppSecret
));
//Facebook Authentication part
$user = $facebook->getUser();
$perms = "manage_pages";
if(isset($_GET['backlink']))
$redirectUrl = urldecode($_GET['backlink']);
else
$redirectUrl = $fullserverpath;
$cancelUrl = $ServerPath."index.php?page=loginWFb";
//AA - where to go to get FB popup.
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => $perms,
'redirect_uri' => $redirectUrl,
'cancel_uri' => $cancelUrl
)
);
//AA- Defining that a powerUSER is someone who's logged in
if(isset($user)){
$_SESSION['loggedIn'] = $user;
}
?>