`

重写的require_login

阅读更多

新的facebook api里面没有之前旧版本的require_login()方法,程序里面用example里面的code有时候会出现不断循环的情况,于是乎在论坛找了一些相关资料之后重写require_login()方法.

 

重写自己的FacebookManager类继承自Facebook

<?php
  require_once(dirname(__FILE__) . "/lib/facebook.php");

  class FacebookManager extends Facebook{

    public function require_login($next_url, $cancel_url = '', $req_perms = '', $optional_perms = ''){

      $fbme = null;

      if ($this->getSession()){
          try
          {
              $fbme = $this->api('/me');
          }
          catch (FacebookApiException $ex)
          {
              error_log($ex, 0);
              //var_dump($ex);
          }
      }

      if (!$fbme)
      {
          $loginUrl = $this->getLoginUrl(array('canvas' => 1,
                                                'fbconnect' => 0,
                                                'req_perms' => $req_perms,
                                                'next' => $next_url,
                                                'cancel_url' => $cancel_url )
                                        );
          ?>
              <script type="text/javascript">
                  /* Change the location if the user is intending to play the game */
                  top.location = '<?php echo($loginUrl); ?>';
              </script>
              <a href="<?php echo($loginUrl); ?>">Please click here if you are not automatically redirected</a>
          <?php
              die();
      }
      else
      {
          //$fbme is valid i.e. user can access our app
      }


      $result_array = array();
      $result_array['user_info'] = $fbme;
      $result_array['ext_perms'] = array();



      if ($fbme && $req_perms != '')
      {
          //Check if all requested extended permissions exist
          //If any permission has not been given, ask for them all. Facebook permissions dialog will
          //skip any that have been given
          foreach ( explode(',', $req_perms) as $perm)
          {
              try
              {
                  $hasAppPermission_perm_result = $this->api
                  (
                      array('method' => 'users.hasAppPermission', 'ext_perm' => $perm)
                  );
              }
              catch (Exception $ex)
              {
                  error_log($ex, 0);
                  //var_dump($ex);
              }

              if ($hasAppPermission_perm_result !== "1")
              {
                  $loginUrl = $this->getLoginUrl(array('canvas' => 1,
                                                        'fbconnect' => 0,
                                                        'req_perms' => $req_perms,
                                                        'next' => $next_url,
                                                        'cancel_url' => $cancel_url )
                                                );
                  ?>
                      <script type="text/javascript">
                          /* Change the location if the user is intending to play the game */
                          top.location = '<?php echo($loginUrl); ?>';
                      </script>
                      <a href="<?php echo($loginUrl); ?>">Please click here if you are not automatically redirected</a>
                  <?php
                      die();
              }
              else
              {
                  $result_array['ext_perms'][$perm] = true;
              }

              unset($hasAppPermission_perm_result);
          }


          
          foreach ( explode(',', $optional_perms) as $perm)
          {
              try
              {
                  $hasAppPermission_perm_result = $this->api
                  (
                      array('method' => 'users.hasAppPermission', 'ext_perm' => $perm)
                  );
              }
              catch (Exception $ex)
              {
                  error_log($ex, 0);
                  //var_dump($ex);
              }

              if ($hasAppPermission_perm_result == "1")
              {
                  $result_array['ext_perms'][$perm] = true;
              }

              unset($hasAppPermission_perm_result);
          }


      }

//        foreach (explode(',', $req_perms) as $perm)
//        {
//            $result_array['ext_perms'][$perm] = true;
//        }

      $result_array['access_token'] = $this->getAccessToken();

      return($result_array);
    }
  }

 

在自己的index页面的示例代码:

 

<?php
  define('APP_ID', 'xxxx');
  define('API_KEY', 'xxxx');
  define('SECRET', 'xxxx');
  require_once("FacebookManager.php");
  $facebook  = new FacebookManager(array(
      'appId'  =>  APP_ID,
      'secret' =>  SECRET,
      'cookie' =>  true
  ));
  $user = $facebook->require_login("http://apps.facebook.com/atlantissocial/","","email,publish_stream,user_birthday");  //permission

  $session = $facebook->getSession();  //$session里面有access_token

  $facebook_id = $facebook->getUser();
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics