您是否启用了 php 错误日志记录?日志说什么?
在没有看到您的 utils.php 的情况下帮助您调试是相当困难的
isset 是一个函数,如果创建了变量并具有值,则返回 true。
根据您的帐户,这意味着您的 $facebook->api('/me') 返回 null 或该行代码根本没有运行。
无法确定您的代码出了什么问题,因为没有看到您在做什么,我只是在猜测。
无论如何,既然您使用的是 PHP,那么首先省略登录按钮怎么样?只需在用户进入应用程序后立即提示用户登录(如果他们尚未登录 Facebook),如果收到 Facebook 会话,向他们展示实际的应用程序,您将已经拥有用户个人资料信息可以使用。那声音怎么样?
这是我通常在我的 Facebook 应用程序中执行的操作。您可以根据自己的需要随意调整它。
配置文件
<?php
//set facebook application id, secret key and api key here
$fbconfig['appid'] = "xxxxxxxxxxxxxxxx";
$fbconfig['secret'] = "xxxxxxxxxxxxxxxxxxxxxxxxx";
$protocol = "http";
if(isset($_SERVER['HTTPS']))
{
if($_SERVER['HTTPS'])
{
$protocol = "https";
}
}
//set application urls here
$domainName = $_SERVER['HTTP_HOST'];
$appNamespace = "[APP_NAMESPACE]";
$appFolder = "[WEB_FOLDER_NAME]";
$fbconfig['appBaseUrl'] = $protocol."://apps.facebook.com/".$appNamespace;
$fbconfig['baseUrl'] = $protocol."://" . $domainName . "/" . $appFolder;
$uid = null; // facebook user id
$me = null; // JSON object containing the basic user information
$redirect_url = $fbconfig['baseUrl']."/redirect.php";
$cancel_url = $protocol."://www.facebook.com";
$allow_url = $fbconfig['appBaseUrl'];
$profileURL = $protocol."://www.facebook.com/profile.php?id=";
?>
重定向.php
<?php
include_once "config.php";
if (isset($_REQUEST["error"]))
{
?>
<script type="text/javascript">
top.location.href = "<?=$cancel_url?>";
</script>
<?php
}
else
{
?>
<script type="text/javascript">
top.location.href = "<?=$allow_url?>";
</script>
<?php
}
?>
fbmain.php
<?php
include_once "../facebook_sdk/facebook.php";
include_once "config.php";
$uid = null; // facebook user id
$me = null; // JSON object containing the basic user information
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
$signedRequest = $facebook->getSignedRequest();
// We may or may not have this data based on a $_GET or $_COOKIE based session.
//
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$user = $facebook->getUser();
$access_token = $facebook->getAccessToken();
$logoutUrl = $facebook->getLogoutUrl();
$loginUrl = $facebook->getLoginUrl(
array(
//state what are the required perms
'redirect_uri' => $redirect_url,
'scope' => 'email, user_likes, user_status, friends_likes, friends_status',
)
);
// Session based API call.
if ($user) {
try {
$me = $facebook->api('/me');
if($me)
{
$uid = $me['id'];
$_SESSION['fbId'] = $me['id'];
}
}
catch (FacebookApiException $e) {
error_log($e);
}
}
else {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
function outputData($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
之后,我只需要在任何需要 facebook 会话的页面中包含“fbmain.php”。我通常会将包含在我的“index.php”中。
索引.php
<?php
include_once "fbmain.php";
?>
<html>
<body id="my_body">
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Canvas.setAutoGrow();
FB.Canvas.scrollTo(0,0);
};
(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
if ($me)
{
echo "Facebook ID: ".$me['id']. "<br />";
echo "Facebook Name: ".$me['name'];
}
?>
</body>
</html>
站点中的后续页面将使用会话变量使用 Javascript SDK 进行 api 调用,而不是再次通过整个身份验证流程。