facebook连接访问令牌保存为php会话中断代码

时间:2015-03-24 16:24:24

标签: php facebook facebook-graph-api

我正在尝试将访问令牌作为php会话传递

发生了一些奇怪的事情,我无法理解为什么。

fbconfig.fb

<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
 FacebookSession::setDefaultApplication( 'xxxxxxxx','xxxxxxxxx' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://www.something.net/triviapp/1353/fbconfig.php' );
try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject();
    $fbid = $graphObject->getProperty('id');              // To Get Facebook ID
    $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
    $firstname = $graphObject->getProperty('first_name');
    $lastname = $graphObject->getProperty('last_name');
    $femail = $graphObject->getProperty('email');    // To Get Facebook email ID
/* ---- Session Variables -----*/
    $_SESSION['FBID'] = $fbid;           
    $_SESSION['FULLNAME'] = $fbfullname;
    $_SESSION['EMAIL'] =  $femail;  
    $_SESSION['firstname'] =  $firstname;
    $_SESSION['lastname'] =  $lastname;

    $accessToken = $session->getAccessToken();
    $_SESSION['accesstoken'] =  $accessToken;


    //echo $_SESSION['accesstoken'];
   /* ---- header location after session ----*/
header("Location: index.php");
} else {
  $loginUrl = $helper->getLoginUrl();
 header("Location: ".$loginUrl);
}
?>
到目前为止一切顺利。 我得到了连接,我获得了访问令牌并将用户发送到index.php

index.php是facebook连接开始的地方,我查看是否有facebook会话。如果我没有将用户发送到上面的文件fbconfig.php,我会将用户发送到html5应用程序(名为mobile.php的文件)

无论如何,当我尝试在mobile.php中访问$ _SESSION [&#39; accesstoken&#39;]时,代码会中断。代码只是在我尝试输出该会话的地方结束,我无法理解为什么。

0 个答案:

没有答案