Facebook连接:javascript日志会话但PHP没有

时间:2012-03-07 21:58:18

标签: php facebook facebook-graph-api facebook-php-sdk facebook-login

我有示例facebook登录实现,并且意识到它已停止工作:

 <?php
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'myappid',
  'secret' => 'mybigsecreet',
  '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');

        $r = new registro_usuarios();
        $r->facebook($uid,$me['name'],'https://graph.facebook.com/'.$me['id'].'/picture');

        echo '----------------------------'.$me;
  } catch (FacebookApiException $e) {
    error_log($e);
    echo '----------------------------'.$e;
  }
}else echo 'nosession';
echo $session;

打印nosesion但是当我点击登录按钮(facebook)时,firebug记录:FB.login() called when user is already connected.并且登录弹出窗口(facebook)将无法打开。

我错过了什么? facebook-api一年后过时了吗?

3 个答案:

答案 0 :(得分:2)

$facebook->getSession()不再有效。这就是为什么它可能会从第一个if循环中退出。

$ session = $ facebook-&gt; getSession();

$ session始终为null。

你可以$facebook->getUser()检查一下,如果它给0则没有活动会话,否则你将得到会话用户的facebook id。

查看此链接也可能获得更多信息:

答案 1 :(得分:1)

您应该在https://github.com/facebook/php-sdk

从Github下载最新的SDK

然后执行以下操作,将记录用户会话。

require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '344617158898614',
  'secret' => '6dc8ac871858b34798bc2488200e503d',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

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

这取自示例代码。对我来说工作得很好。

答案 2 :(得分:0)

自10月1日开始,OAuth 2.0身份验证是强制性的(请参阅此处的更改路线图:https://developers.facebook.com/roadmap/completed-changes/),您必须更改一些不再受支持的功能。

您可以在此处查看新的php文档:https://developers.facebook.com/docs/reference/php/

以及身份验证文档: https://developers.facebook.com/docs/authentication/