你是否啓用了PHP錯誤日誌記錄?日誌說什麼?
它相當困難,以幫助您調試沒有看到你的utils.php中
isset是如果創建一個變量,它返回true,具有值的函數。
通過您的帳戶,這意味着您的$ facebook-> api('/ me')返回null或該行代碼根本沒有運行。
不知道你的代碼出了什麼問題,因爲沒有看到你在做什麼,我只是在猜測。
無論如何,既然您使用的是PHP,那麼在第一個地方如何省略登錄按鈕呢?只要他們一進入應用程序就提示用戶登錄(如果他們沒有登錄到Facebook),如果收到Facebook會話,向他們展示實際的應用程序,並且您已經擁有用戶個人資料信息。那聲音怎麼樣?
這是我通常在我的Facebook應用程序中做的事情。你可以用你喜歡的任何方式調整它來滿足你的需求。
的config.php
<?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=";
?>
redirect.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」中。在現場
的index.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調用。
我想你的'$ user_profile'變量沒有設置,呵呵?請告訴我們您設置變量的位置。 – sebastian 2013-05-08 13:38:02
@sebastian我有一個名爲'utils.php'的文件,我在那裏設置了'$ user_profile'。我用'require_once'使它識別變量。 – 2013-05-08 13:43:46