Facebook PHP SDK exception error

前端 未结 3 1734
-上瘾入骨i
-上瘾入骨i 2021-01-13 07:12

I am getting an interesting SDK exception message from FB\'s PHP SDK. I set up my code exactly as the FB docs say to do..?

DEBUG ERROR MESSAGE:

[12-J         


        
相关标签:
3条回答
  • 2021-01-13 07:38

    Put session_start(); on the TOP of your file

    0 讨论(0)
  • 2021-01-13 07:53

    To expand Chancho's answer, the SDK will check to see if a session is active before it tries to store any data to it. There are 3 ways around this.

    Start a native PHP session

    If you're just using PHP's native sessions, Chancho's answer will suffice. You have to make sure that session_start(); is at the very top of your script before any output is sent.

    Overwrite the session handling

    If you're using a web framework like Codeigniter, Yii, CakePHP, Laravel, etc, they usually have their own ways of storing a session. To overwrite the default session store, you'll need to write your own class that extends the \Facebook\FacebookRedirectLoginHelper class. Then you'll need to overwrite the storeState() and loadState() methods. These are the only two methods in the whole SDK that interface with the $_SESSION variable.

    You can see a laravel implementation example in the GitHub issues for the SDK. Here's the laravel implementation:

    class MyFacebookRedirectLoginHelper extends \Facebook\FacebookRedirectLoginHelper
    {
      protected function storeState($state)
      {
        Session::put('state', $state);
      }
    
      protected function loadState()
      {
        return $this->state = Session::get('state');
      }
    }
    

    Disable the session check completely

    If you're trying to use the SDK in a stateless environment like the command line, there's no point in starting a session or overwriting how the session data is stored. You can simply disable the check with FacebookRedirectLoginHelper::disableSessionStatusCheck().

    To disable the session check just run this.

    $helper = new FacebookRedirectLoginHelper('*******');
    
    $helper->disableSessionStatusCheck();
    
    $session = $helper->getSessionFromRedirect();
    
    // . . . 
    

    I hope that helps!

    0 讨论(0)
  • 2021-01-13 07:57

    For using CakePHP v3.1.6 + Facebook SDK v4.1.

    In the newer SDK, \Facebook\FacebookRedirectLoginHelper class has been changed to use \Facebook\FacebookSessionPersistentDataHandler class to handle session "state". Putting $this->request->session()->start() in AppController initialize will fix the problem using Facebook SDK with AuthComponent.

    public function initialize() {
        parent::initialize();
        $this->request->session()->start();
    
        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
    
        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'fields' => ['username' => 'email', 'password' => 'password']
                ]
            ],
            'loginAction' => ['controller' => 'Users', 'action' => 'login']
        ]);
    }
    
    0 讨论(0)
提交回复
热议问题