重写的require_login

由于新的Facebook API不再包含require_login()方法,导致程序中使用旧代码时出现循环问题。作者通过研究论坛资料,重写了FacebookManager类并继承自Facebook,解决了这一问题。
摘要由CSDN通过智能技术生成

新的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();
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值