$facebook->getSession();不工作。不知道为什么?

问题描述 投票:0回答:4
 <?php

require 'facebook.php';

// Create our Application instance.
$facebook = new Facebook(array(
  'appId'  => '130407366991766',
  'secret' => '****',
  '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();

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

// login or logout url will be needed depending on current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>

<h1><a href="example.php">php-sdk</a></h1>

<?php if ($me): ?>
<a href="<?php echo $logoutUrl; ?>">
  <img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
</a>
<?php else: ?>
<a href="<?=$loginUrl?>">
  <img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">
</a>
<?php endif ?>

<h3>Session</h3>
<?php if ($me): ?>
<pre><?php print_r($session); ?></pre>

<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $uid; ?>/picture">
<?php echo $me['name']; ?>

<h3>Your User Object</h3>
<pre><?php print_r($me); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>

<h3>Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>

由于某种原因...虽然登录似乎工作正常,但我的脚本不断显示“您尚未连接”。

我只能假设这是我的应用程序设置的问题。大家有什么想法吗?

演示:http://gua.com/fb-api/

php facebook facebook-php-sdk
4个回答
4
投票

我也遇到了完全相同的问题。我的整个应用程序被破坏了,因为 getSession() 不再起作用。

我下载了最新版本的 PHP SDK 并使用示例来初始化用户会话。

基本上,一旦创建 Facebook 对象,会话就会被初始化。因此,从示例代码来看,当您调用时。

$facebook = new Facebook(array(
  'appId'  => '191149314281714',
  'secret' => '73b67bf1c825fa47efae70a46c18906b',
));

您已经有一个会话。要获取用户 ID,您只需调用:

$uid = $facebook->getUser();

如果该调用的结果是

0
,则用户未登录。


3
投票

Facebook 更改为 OAuth2 和 GetSession 不起作用从开发人员 PHP Sdk 下载新的 base_facebook.php 和 facebook.php 并将登录过程替换为:

if( $userfb = $facebook->getUser() ){ 
   try{
        $UserProfile = $facebook->api('/me');
   }catch (FacebookApiException $e) {
      $userfb      = null;
      $UserProfile = null;
}

在我的网站上工作,新版本无法显示带有权限参数的 fb:login-button :( 并且我无法修复


2
投票

Facebook 改变了您调用 API 的方式,并且旧的 REST API 调用已被弃用。这意味着 getSession() 方法不再起作用。


-1
投票

您需要将 Api Key 提供给构造函数。不是应用程序 ID。

© www.soinside.com 2019 - 2024. All rights reserved.