$facebook->getSession(); not working. No idea why?

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

 <?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/

回答1:

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.



回答2:

Facebook Change to OAuth2 and GetSession not working download new base_facebook.php and facebook.php from developers PHP Sdk and replace login procedure to:

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

Is work in my site the new version can't show a fb:login-button with perms parameter :( and i can't repair



回答3:

Facebook have changed the way you call the API and the old REST API calls are being depracated. This means that the getSession() method doesn't work anymore.



回答4:

You need to give the Api Key to the constructor. Not the App Id.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!