那么,你首先需要验证,如果你有一个积极的访问令牌。如果不是,您应该显示Facebook登录按钮。
<?php
$userId = $facebook -> getUser();
if ($userId) {
$userInfo = $facebook->api('/' . $userId); ?>
Welcome <?= $userInfo['name'] ?>
<?php } else { ?>
<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1"></div>
<?php } ?>
根据你的代码应该是这样的:
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
?>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxx', // App ID
channelUrl : 'http://YOURDOMAIN.com/channel.php', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
//alert(me.name);
});
}
</script>
<?php
$userId = $facebook -> getUser();
if ($userId) {
$userInfo = $facebook->api('/' . $userId); ?>
Welcome <?= $userInfo['name'] ?>
<?php } else { ?>
<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1"></div>
<?php } ?>
<script>
// Load the SDK Asynchronously
(function(d) {
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement('script');
js.id = id;
js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
你应该有一个channel.php文件在您的服务器,这将有以下内容:
<?php
$cache_expire = 60*60*24*365;
header("Pragma: public");
header("Cache-Control: max-age=".$cache_expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT');
?>
<script src="//connect.facebook.net/en_US/all.js"></script>
您是否对用户进行了身份验证并请求了基本权限? – Lix
我相信,由于用户的基本信息是公开的,所以不需要申请姓名或姓氏的许可。至少这就是Facebook所说的。 – user1816160
那么你肯定是错了我的朋友。就应用程序而言,**没有身份验证和基本权限,应用程序不知道谁在查看它。**当以用户身份查看Facebook并通过应用程序查看API中的数据时,隐私存在差异。 – Lix