PHP facebook SDK 4.0 login error

后端 未结 2 763
我在风中等你
我在风中等你 2021-01-27 13:51

I was looking for docs on official page but there is no anything helpful, so i build this code and it don\'t work.

    FacebookSession::setDefaultApplication(\'a         


        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-27 14:21

    Steps to avoiding PHP facebook SDK 4.0 login error

    1. First install composer in your PC if your are using windows OS.
    2. Download facebook SDK 4.0 and rename what ever you want (Like: fbsdk).
    3. Run Command prompt and locate your recently downloaded sdk folder.
    4. Check if there are composer.json file exist or not
    5. if composer.json file exist then type this command: composer install otherwise you have create new file composer.json in same folder with following code: { "require" : { "facebook/php-sdk-v4" : "4.0.*" } }
    6. If you will type correct command as above mentioned in line number 5 then you will get this message : Loading composer repositories with package information Installing dependencies (including require-dev).
    7. Create index.php file and include autoload.php top of the file.
    8. For example follow the sample codes for login with facebook

      session_start();
      
      require_once('/home3/users/public_html/fbsdk/vendor/autoload.php');
      
      use Facebook\FacebookSession;
      
      use Facebook\FacebookRequest;
      
      use Facebook\GraphUser;
      
      use Facebook\FacebookRequestException;
      
      use Facebook\FacebookRedirectLoginHelper;
      
      FacebookSession::setDefaultApplication(YOUR APP_ID, YOUR SECRET_KEY);
      
      $helper = new FacebookRedirectLoginHelper('http://example.com/fbsdk/login.php');
      
      if(isset($_SESSION['access_token'])) {
      
          $access_token = $_SESSION['access_token'];
      
          $session = new FacebookSession($access_token);
      
      } else {
      
          unset($_SESSION['access_token']);
      
          try {
      
              $session = $helper->getSessionFromRedirect();
      
              if($session)                    
                  $_SESSION['access_token'] = $session->getToken();
      
          } catch(FacebookRequestException $ex) {
      
              // When Facebook returns an error
      
          } catch(Exception $ex) {
      
              // When validation fails or other local issues
      
          }
      
      }
      
      if ($session) {
      
          $request = new FacebookRequest($session, 'GET', '/me');
          $response = $request->execute();
          $graphObject = $response->getGraphObject();
      
          header('Location: ./home.php');
      
      } else {
      
          $loginUrl = $helper->getLoginUrl();
      
          header('Location: ' . $loginUrl);
      
      }
      

提交回复
热议问题