我有如下代码。为什么$userId
总是0
?当我单击登录按钮时,我得到B.login() called when user is already connected.
. 当我在 fb 检查允许的应用程序时,会添加此应用程序。看起来我已连接,所以它应该是 myid
而不是0
.
<pre>
define('YOUR_APP_ID', ###);
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => ###,
'secret' => '###',
));
$userId = $facebook->getUser();
echo $userId;
?>
<html>
<body>
<?php if ($userId) {
$userInfo = $facebook->api('/' + $userId); ?>
Welcome <?= $userInfo['name'] ?>
<?php } else { ?>
<div id="fb-root"></div>
<fb:login-button></fb:login-button>
<?php } ?>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<? echo('####'); ?>',
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
</body>
</html>