php QQ第三方登录/OAuth2.0验证

qq实现第三方网站登录

创建QQ互联账号

可用QQ号码登录 登录地址:http://connect.qq.com/

QQ互联->管理中心->创建应用

1.创建应用

2.创建网站

注意内容:网站地址一定要加入图中所标注的信息


创建成功

获取 APP ID 和  APP KEY

header请求

  1. public function actionQQ()  
  2.     {  
  3. //$redirect 为回调地址  $app_id 应用编号  
  4.  $url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=' . $app_id . '&redirect_uri=' . $redirect;  
  5.         header('Location:' . $url);  
  6.     }  
public function actionQQ()
    {
//$redirect 为回调地址  $app_id 应用编号
 $url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=' . $app_id . '&redirect_uri=' . $redirect;
        header('Location:' . $url);
    }

获取QQ用户信息

  1. <?php  
  2.   
  3. class QQ_sdk  
  4. {  
  5.     private $app_id = '101****0572';  
  6.     private $app_secret = 'e55264******132366';  
  7.     private $redirect = 'http://www.***.cn/';  
  8.   
  9.     function __construct()  
  10.     {  
  11.   
  12.     }  
  13.   
  14.     /** 
  15.      * [get_open_id 获取用户唯一ID,openid] 
  16.      * @param [string] $token [授权码] 
  17.      * @return [array] [成功返回client_id 和 openid ;失败返回error 和 error_msg] 
  18.      */  
  19.     function get_open_id($token)  
  20.     {  
  21.         $str = $this->curl_get_content('https://graph.qq.com/oauth2.0/me?access_token=' . $token);  
  22.         if (strpos($str"callback") !== false) {  
  23.             $lpos = strpos($str"(");  
  24.             $rpos = strrpos($str")");  
  25.             $str = substr($str$lpos + 1, $rpos - $lpos - 1);  
  26.         }  
  27.         $user = json_decode($str, TRUE);  
  28.         return $user;  
  29.     }  
  30.   
  31.     /** 
  32.      * [get_access_token 获取access_token] 
  33.      * @param [string] $code [登陆后返回的$_GET['code']] 
  34.      * @return [array] [expires_in 为有效时间 , access_token 为授权码 ; 失败返回 error , error_description ] 
  35.      */  
  36.     function get_access_token($code)  
  37.     {  
  38.         $token_url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&'  
  39.             . 'client_id=' . $this->app_id . '&redirect_uri=' . urlencode($this->redirect) . '&client_secret=' . $this->app_secret . '&code=' . $code;  
  40.         $token = array();  
  41.         parse_str($this->curl_get_content($token_url), $token);  
  42.         return $token;  
  43.   
  44.     }  
  45.   
  46.     /** 
  47.      * [get_user_info 获取用户信息] 
  48.      * @param [string] $token [授权码] 
  49.      * @param [string] $open_id [用户唯一ID] 
  50.      * @return [array] [ret:返回码,为0时成功。msg为错误信息,正确返回时为空。...params] 
  51.      */  
  52.     function get_user_info($token$open_id)  
  53.     {  
  54.         $user_info_url = 'https://graph.qq.com/user/get_user_info?' . 'access_token=' . $token . '&oauth_consumer_key=' . $this->app_id . '&openid=' . $open_id . '&format=json';  
  55.         $info = json_decode($this->curl_get_content($user_info_url), TRUE);  
  56.         return $info;  
  57.     }  
  58.   
  59.     private function curl_get_content($url)  
  60.     {  
  61.         $ch = curl_init();  
  62.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
  63.         curl_setopt($ch, CURLOPT_URL, $url);  
  64.         //设置超时时间为3s  
  65.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);  
  66.         $result = curl_exec($ch);  
  67.         curl_close($ch);  
  68.         return $result;  
  69.     }  
  70. }  
<?php

class QQ_sdk
{
    private $app_id = '101****0572';
    private $app_secret = 'e55264******132366';
    private $redirect = 'http://www.***.cn/';

    function __construct()
    {

    }

    /**
     * [get_open_id 获取用户唯一ID,openid]
     * @param [string] $token [授权码]
     * @return [array] [成功返回client_id 和 openid ;失败返回error 和 error_msg]
     */
    function get_open_id($token)
    {
        $str = $this->curl_get_content('https://graph.qq.com/oauth2.0/me?access_token=' . $token);
        if (strpos($str, "callback") !== false) {
            $lpos = strpos($str, "(");
            $rpos = strrpos($str, ")");
            $str = substr($str, $lpos + 1, $rpos - $lpos - 1);
        }
        $user = json_decode($str, TRUE);
        return $user;
    }

    /**
     * [get_access_token 获取access_token]
     * @param [string] $code [登陆后返回的$_GET['code']]
     * @return [array] [expires_in 为有效时间 , access_token 为授权码 ; 失败返回 error , error_description ]
     */
    function get_access_token($code)
    {
        $token_url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&'
            . 'client_id=' . $this->app_id . '&redirect_uri=' . urlencode($this->redirect) . '&client_secret=' . $this->app_secret . '&code=' . $code;
        $token = array();
        parse_str($this->curl_get_content($token_url), $token);
        return $token;

    }

    /**
     * [get_user_info 获取用户信息]
     * @param [string] $token [授权码]
     * @param [string] $open_id [用户唯一ID]
     * @return [array] [ret:返回码,为0时成功。msg为错误信息,正确返回时为空。...params]
     */
    function get_user_info($token, $open_id)
    {
        $user_info_url = 'https://graph.qq.com/user/get_user_info?' . 'access_token=' . $token . '&oauth_consumer_key=' . $this->app_id . '&openid=' . $open_id . '&format=json';
        $info = json_decode($this->curl_get_content($user_info_url), TRUE);
        return $info;
    }

    private function curl_get_content($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url);
        //设置超时时间为3s
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}

可在设置<meta property="qc:admins" content="756420************1316367" />目录下测试

  1. if(isset($_GET['code'])) {  
  2.         //qq获取信息  
  3.          include('QQ_sdk.php');  
  4.          $qq_sdk = new Qq_sdk();   
  5.          $token = $qq_sdk->get_access_token($_GET['code']);   
  6.          echo "<pre>";  
  7.          print_r($token)."</br>";   
  8.          echo "</pre>";  
  9.              $open_id = $qq_sdk->get_open_id($token['access_token']);   
  10.          echo "<pre>";  
  11.          print_r($open_id)."</br>";   
  12.          echo "</pre>";  
  13.     }         
  14.     ?>  
if(isset($_GET['code'])) {
		//qq获取信息
		 include('QQ_sdk.php');
		 $qq_sdk = new Qq_sdk(); 
		 $token = $qq_sdk->get_access_token($_GET['code']); 
		 echo "<pre>";
		 print_r($token)."</br>"; 
		 echo "</pre>";
	         $open_id = $qq_sdk->get_open_id($token['access_token']); 
		 echo "<pre>";
		 print_r($open_id)."</br>"; 
		 echo "</pre>";
	}		
	?>

获取用户信息


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值