$ facebook->的getSession();不工作。不知道为什么? [英] $facebook->getSession(); not working. No idea why?

查看:115
本文介绍了$ facebook->的getSession();不工作。不知道为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?php 

需要'facebook.php';

//创建我们的应用程序实例。
$ facebook = new Facebook(array(
'appId'=>'130407366991766',
'secret'=>'****',
'cookie' => true,
));

//我们可能会也可能不会有基于$ _GET或$ _COOKIE的会话。
//
//如果我们在这里得到一个会话,这意味着我们使用
//仅应用程序的秘密Facebook和应用程序知道找到了正确签名的会话。我们不知道
//如果它仍然有效,直到我们使用会话进行API调用。会话
//如果已经过期(在这种情况下不应该获得
//会话),或者用户已注销Facebook,则会失效。
$ session = $ facebook-> getSession();

$ me = null;
//基于会话的API调用。
if($ session){
try {
$ uid = $ facebook-> getUser();
$ me = $ facebook-> api('/ me');
} catch(FacebookApiException $ e){
error_log($ e);
}
}

//根据当前用户状态,将需要登录或注销url。
if($ me){
$ logoutUrl = $ facebook-> getLogoutUrl();
} else {
$ loginUrl = $ facebook-> getLoginUrl();
}

//由于我们正在获取公共数据,因此此调用将始终有效。
$ 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>会话< / h3>
<?php if($ me):?>
< pre><?php print_r($ session); ?>< /预>

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

< h3>您的使用者物件< / h3>
< pre><?php print_r($ me); ?>< /预>
<?php else:?>
< strong>< em>您没有连接。< / em>< / strong>
<?php endif?>

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




理由......虽然登录似乎是完美的,但我的脚本持续说你没有连接。



我只能假设这是我的应用程序的问题建立。任何人有任何想法?



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

解决方案

我有完全相同的问题。我的整个应用程序被中断,因为getSession()不再有效。



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

基本上,只要创建Facebook对象,会话就会初始化。

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

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

  $ uid = $ facebook-> getUser(); 

如果该调用的结果是 0 ,那么用户没有登录。


 <?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']; ?>

For some reason... although the login seems to be working perfectly, my script continuously states "You are not connected".

I can only assume it is a problem with my application setup. Anyone have any ideas?

Demo: http://gua.com/fb-api/

解决方案

I had exactly the same problem. My whole app was broken because getSession() was no longer functioning.

I downloaded the latest version of the PHP SDK and used the example to initialize the user session.

Basically, the session is initialized as soon as you create the Facebook object. So from the example code, when you call.

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

You already have a session. To get the user ID, you just call:

$uid = $facebook->getUser();

If the result of that call is 0, then the user is not logged in.

这篇关于$ facebook-&GT;的getSession();不工作。不知道为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆