php - $facebook->getSession() 在示例代码中返回 null。那样行吗?

标签 php facebook

运行 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 &amp; XFBML: <fb:login-button></fb:login-button>
</div>
<div>
Without using JavaScript &amp; 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/

相关文章:

ios - 如何测试我的 facebook 好友列表功能是否在 swift 中工作

javascript - window.location.href 在phonegap应用程序中不起作用

php显示来自mysql的数据

PHPExcel 保存 PDF

php - 在 PHP 页面中重定向到另一个 PHP 页面

php - Mysql 上的导航栏?还是坏主意?

php - 用于 php 单元测试的理想 stub 实用程序

html - Facebook 如何阻止您将网站放入 iFrame?

facebook - 使用 facebook graph api 从页面获取事件

Facebook 给了我一个 "URL requested a HTTP redirect, but it could not be followed."但浏览器跟随它没有问题,我该如何解决?