QQ登陆类

jerry PHP 2015年11月18日 收藏
qq登陆类已封装,用于QQ登陆的callback页面
  1. <?php

  2. /**
  3.  * qq登陆接口类
  4.  * 实例化类时传入3个参数 app_id,app_key,callback
  5.  * qq接入流程需要自己去QQ互联文档上了解,此类只做回调功能封装
  6.  * arthur:米国村长
  7.  * */

  8. class qqlogin{
  9.     public $app_id;
  10.     public $app_key;
  11.     public $callback;
  12.     public $code;
  13.     public $state;


  14.     public function __construct($app_id,$app_key,$callback){
  15.         //接收从qq登陆页返回来的值
  16.         $this->code = isset($_REQUEST['code'])? $_REQUEST['code'] : '';
  17.         $this->state = isset($_REQUEST['state'])? $_REQUEST['state'] : '';
  18.         //将参数赋值给成员属性
  19.         $this->app_id = $app_id;
  20.         $this->app_key = $app_key;
  21.         $this->callback = $callback;
  22.     }

  23.     /**
  24.      * 获取access_token值
  25.      * @return array 返回包含access_token,过期时间的数组
  26.      * */
  27.     public function get_token(){
  28.         $url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=".$this->app_id."&client_secret=".$this->app_key."&code=".$this->code."&redirect_uri=".urlencode($this->callback);
  29.         $str = $this->visit_url($url);//访问url获得返回值
  30.         parse_str($str,$arr);
  31.         return $arr;
  32.     }

  33.     /**
  34.      * 获取client_id 和 openid
  35.      * @param $access_token access_token验证码
  36.      * @return array 返回包含client_id 和 openid的数组
  37.      * */
  38.     public function get_client_id($access_token){
  39.         $url = 'https://graph.qq.com/oauth2.0/me?access_token='.$access_token;
  40.         $str = $this->visit_url($url);//访问url获得返回值
  41.         return $this->change_callback($str);//返回经过json转码后的数组
  42.     }

  43.     /**
  44.      * 获取用户信息
  45.      * @param $client_id
  46.      * @param $access_token
  47.      * @param $openid
  48.      * @return array 用户的信息数组
  49.      * */
  50.     public function user_info($client_id,$openid,$access_token){
  51.         $url = 'https://graph.qq.com/user/get_user_info?oauth_consumer_key='.$client_id.'&access_token='.$access_token.'&openid='.$openid.'&format=json';
  52.         $str = $this->visit_url($url);
  53.         $arr = json_decode($str,true);
  54.         return $arr;

  55.     }

  56.     /**
  57.      * 请求URL地址,得到返回字符串
  58.      * @param $url qq提供的api接口地址
  59.      * */
  60.     public function visit_url($url){
  61.         static $cache = 0;
  62.         //判断是否之前已经做过验证
  63.         if($cache === 1){
  64.             $str = $this->curl($url);
  65.         }elseif($cache === 2){
  66.             $str = $this->openssl($url);
  67.         }else{
  68.             //是否可以使用cURL
  69.             if(function_exists('curl_init')){
  70.                 $str = $this->curl($url);
  71.                 $cache = 1;
  72.                 //是否可以使用openssl
  73.             }elseif(function_exists('openssl_open') && ini_get("allow_fopen_url")=="1"){
  74.                 $str = $this->openssl($url);
  75.                 $cache = 2;
  76.             }else{
  77.                 die('请开启php配置中的php_curl或php_openssl');
  78.             }
  79.         }
  80.         return $str;
  81.     }


  82.     /**
  83.      * 将字符串转换为可以进行json_decode的格式
  84.      * 将转换后的参数值赋值给成员属性$this->client_id,$this->openid
  85.      * @param $str 返回的callback字符串 
  86.      * @return 数组
  87.      * */
  88.     protected function change_callback($str){
  89.         if (strpos($str, "callback") !== false){
  90.             //将字符串修改为可以json解码的格式
  91.             $lpos = strpos($str, "(");
  92.             $rpos = strrpos($str, ")");
  93.             $json  = substr($str, $lpos + 1, $rpos - $lpos -1);
  94.             //转化json
  95.             $result = json_decode($json,true);
  96.             $this->client_id = $result['client_id'];
  97.             $this->openid = $result['openid'];
  98.             return $result;

  99.         }else{
  100.             return false;
  101.         }
  102.     }

  103.     /**
  104.      * 通过curl取得页面返回值
  105.      * 需要打开配置中的php_curl
  106.      * */
  107.     private function curl($url){
  108.         $ch = curl_init();
  109.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);//允许请求的内容以文件流的形式返回
  110.         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//禁用https
  111.         curl_setopt($ch,CURLOPT_URL,$url);//设置请求的url地址
  112.         $str = curl_exec($ch);//执行发送
  113.         curl_close($ch);
  114.         return $str;
  115.     }

  116.     /**
  117.      * 通过file_get_contents取得页面返回值
  118.      * 需要打开配置中的allow_fopen_url和php_openssl
  119.      * */
  120.     private function openssl($url){
  121.         $str = file_get_contents($url);//取得页面内容
  122.         return $str;
  123.     }
  124. }

  125. //必须申请开通QQ登陆,并且自己有域名才可以使用呢~~~
  126. /*实例开始*/
  127. header('content-type:text/html;charset=utf-8');

  128. //申请QQ互联后得到的APP_ID 和 APP_KEY
  129. $app_id = *****9677;
  130. $app_key = '863b3eec66**************';

  131. //回调接口,接受QQ服务器返回的信息的脚本
  132. $callback = 'http://yoursite/xx/callback.php';

  133. //实例化qq登陆类,传入上面三个参数
  134. $qq = new qqlogin($app_id,$app_key,$callback);

  135. //得到access_token验证值
  136. $arr = $qq->get_token();
  137. if(isset($arr['access_token']))
  138.     $access_token = $arr['access_token'];
  139. else
  140.     die('登陆失败');

  141. //得到用户的openid(登陆用户的识别码)和Client_id
  142. $arr = $qq->get_client_id($access_token);
  143. if(isset($arr['client_id'])){
  144.     $client_id = $arr['client_id'];
  145.     $openid = $arr['openid'];
  146. }else{
  147.     die('登陆失败');
  148. }
  149. //请求接口,得到用户所有数据
  150. $arr = $qq->user_info($client_id,$openid,$access_token);

  151. //var_dump($arr);

  152. /*实例结束*/
  153. ?>