基于Thinkphp3.2的qq第三方oauth认证登录扩展类

基于Thinkphp3.2的qq第三方oauth认证登录扩展类,
由于腾讯oauth sdk写的太多,不能与thinkphp和好的结合,最终想法讲腾讯oauth sdk写成tp的扩展类
先看代码,将代码保存在/library/org/util/Qqconnect.class.php文件中
在__construct方法中你可以直接写你的app_id、app_key和回调地址
也可以根据自己的喜好,改一下代码传参或者写到配置文件。
调用方法:1. 在qq的登录按钮的方法中调用getAuthCode方法
例如:
$qqobj=new \Org\Util\Qqconnect();
$qqobj->getAuthCode();

2.在回调地址的方法中调用getUsrInfo方法
例如:
$qqobj=new \Org\Util\Qqconnect();
$result=$qqobj->getUsrInfo();
就可以了

3.getAuthCode方法中的参数scope根据自身需要添加值get_user_info,list_album,upload_pic,do_like。

 

  1. <?php
  2.  // +----------------------------------------------------------------------
  3.  // | Copyright (c) 2015.
  4.  // +----------------------------------------------------------------------
  5.  // | 
  6.  // +----------------------------------------------------------------------
    1. namespace Org\Util;
    2.  /**
    3.  *  qq第三方登录认证
    4.  */
    5.  class Qqconnect {
    6.     private static $data;
    7.     //APP ID
    8.     private $app_id="";
    9.     //APP KEY
    10.     private $app_key="";
    11.     //回调地址
    12.     private $callBackUrl="";
    13.     //Authorization Code
    14.     private $code="";
    15.     //access Token
    16.     private $accessToken="";
    17.     public function __construct(){
    18.         $this->app_id="";
    19.         $this->app_key="";
    20.         $this->callBackUrl="";
    21.         //检查用户数据
    22.         if(empty($_SESSION['QC_userData'])){
    23.             self::$data = array();
    24.         }else{
    25.             self::$data = $_SESSION['QC_userData'];
    26.         }
    27.     }
      1. //获取Authorization Code
      2.     public function getAuthCode(){
      3.         $url="https://graph.qq.com/oauth2.0/authorize";
      4.         $param['response_type']="code";
      5.         $param['client_id']=$this->app_id;
      6.         $param['redirect_uri']=$this->callBackUrl;
      7.         //-------生成唯一随机串防CSRF攻击
      8.                 $state = md5(uniqid(rand(), TRUE));
      9.                 $_SESSION['state']=$state;
      10.         $param['state']=$state;
      11.                 $param['scope']="get_user_info";
      12.         $param =http_build_query($param,'','&');
      13.         $url=$url."?".$param;
      14.         header("Location:".$url);
      15.     }
      16.     //通过Authorization Code获取Access Token
      17.     private function getAccessToken(){
      18.         $url="https://graph.qq.com/oauth2.0/token";
      19.         $param['grant_type']="authorization_code";
      20.         $param['client_id']=$this->app_id;
      21.         $param['client_secret']=$this->app_key;
      22.         $param['code']=$this->code;
      23.         $param['redirect_uri']=$this->callBackUrl;
      24.         $param =http_build_query($param,'','&');
      25.         $url=$url."?".$param;
      26.         return $this->getUrl($url);
      27.     }
      28.     //获取openid
      29.     private function getOpenID(){
      30.         $rzt=$this->getAccessToken();
      31.         parse_str($rzt,$data);
      32.         $this->accessToken=$data['access_token'];
      33.         $url="https://graph.qq.com/oauth2.0/me";
      34.         $param['access_token']=$this->accessToken;
      35.         $param =http_build_query($param,'','&');
      36.         $url=$url."?".$param;
      37.         $response=$this->getUrl($url);
      38.         //--------检测错误是否发生
      39.         if(strpos($response, "callback") !== false){
      40.             $lpos = strpos($response, "(");
      41.             $rpos = strrpos($response, ")");
      42.             $response = substr($response, $lpos + 1, $rpos - $lpos -1);
      43.         }
      44.         $user = json_decode($response);
      45.         if(isset($user->error)){
      46.             exit("错误代码:100007");
      47.         }
      48.         return $user->openid;
      49.     }
      50.     //获取信息
      51.     public function getUsrInfo(){
      52.         if($_GET['state'] != $_SESSION['state']){
      53.             exit("错误代码:300001");
      54.         }
      55.         $this->code=$_GET['code'];
      56.         $openid=$this->getOpenID();
      57.         if(empty($openid)){
      58.             return false;
      59.         }
      60.         $url="https://graph.qq.com/user/get_user_info";
      61.         $param['access_token']=$this->accessToken;
      62.         $param['oauth_consumer_key']=$this->app_id;
      63.         $param['openid']=$openid;
      64.         $param =http_build_query($param,'','&');
      65.         $url=$url."?".$param;
      66.         $rzt=$this->getUrl($url);
      67.         return $rzt;
      68.     }
      69.     //CURL GET
      70.     private function getUrl($url){
      71.         $ch = curl_init($url);
      72.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      73.         curl_setopt($ch, CURLOPT_TIMEOUT, 5);
      74.         if (!empty($options)){
      75.             curl_setopt_array($ch, $options);
      76.         }
      77.         $data = curl_exec($ch);
      78.         curl_close($ch);
      79.         return $data;
      80.     }
      81.     
      82.     //CURL POST
      83.     private function postUrl($url,$post_data){
      84.         $ch = curl_init();
      85.         curl_setopt($ch, CURLOPT_POST, 1);
      86.         curl_setopt($ch, CURLOPT_URL, $url);
      87.         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
      88.         ob_start();
      89.         curl_exec($ch);
      90.         $result = ob_get_contents();
      91.         ob_end_clean();
      92.         return $result;
      93.     }
      94.  }

 

posted @ 2016-09-06 09:39  chinalorin'blogs  阅读(435)  评论(0编辑  收藏  举报