运行 example code for the Facebook API我得到一个空 session 对象,我应该得到一个非空对象,在代码中给出注释。 我做错了什么?
换句话说,在我的 index.php 中,当我转到 http://apps.facebook.com/my_app
时,示例代码中的这个片段显示“无 session ”在我的浏览器中:
<?php
require './facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => '...', // actual value replaced by '...' for this post
'secret' => '...', // actual value replaced by '...' for the post
'cookie' => true,
));
// 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.
$session = $facebook->getSession();
if ($session) {
echo "session ok";
}
else {
echo "no session";
}
?>
注意:在我的服务器中,index.php 和 facebook.php 在同一个文件夹中。
最佳答案
您是否关联了您的应用?
if ($session) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
<?php if ($me): ?>
<a href="<?php echo $logoutUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
</a>
<?php else: ?>
<div>
Using JavaScript & XFBML: <fb:login-button></fb:login-button>
</div>
<div>
Without using JavaScript & XFBML:
<a href="<?php echo $loginUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">
</a>
</div>
<?php endif ?>
代码取自您提供的链接。
关于php - $facebook->getSession() 在示例代码中返回 null。那样行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2804824/