Home > PHP Framework > ThinkPHP > body text

QQ third-party authentication login extension class based on Thinkphp3.2

coldplay.xixi
Release: 2020-06-09 12:04:11
forward
2835 people have browsed it

QQ third-party authentication login extension class based on Thinkphp3.2

QQ third-party authentication login extension class based on Thinkphp3.2

Based on Thinkphp3.2 QQ third-party authentication login extension class. In fact, the following classes are also collected and compiled by me from the TP official website. I have slightly modified and improved them.

Here I put the file in "/Application/Common/Lib/Qqconnect.class.php". (In fact, you can put this file path according to your own preference)

Instantiation

$Qqconnect = new \Common\Lib\Qqconnect();
Copy after login

In the __construct method, you can directly write your app_id, app_key and callback address
You can also change the code to pass parameters or write it to the configuration file according to your own preferences.

Calling method:

1. Call the getAuthCode method in the login button method of qq

$qqobj=new \Org\Util\Qqconnect();
$qqobj->getAuthCode();
Copy after login

2. Call the getUsrInfo method in the callback address method

$qqobj=new \Org\Util\Qqconnect();
$result=$qqobj->getUsrInfo();
Copy after login

3. The parameter scope in the getAuthCode method adds the values ​​​​get_user_info, list_album, upload_pic, do_like according to its own needs.

Qqconnect.class.php


// +----------------------------------------------------------------------
namespace Common\Lib;
/**
 *  qq第三方登录认证
 */
class Qqconnect {
    private static $data;
    //APP ID
    private $app_id="";
    //APP KEY
    private $app_key="";
    //回调地址
    private $callBackUrl="";
    //Authorization Code
    private $code="";
    //access Token
    private $accessToken="";
    private $openid="";
 
    public function __construct(){
        $this->app_id="";
        $this->app_key="";
        $this->callBackUrl=""; //你的回调地址
        //检查用户数据
        if(empty($_SESSION['QC_userData'])){
            self::$data = array();
        }else{
            self::$data = $_SESSION['QC_userData'];
        }
    }
 
 
    //获取Authorization Code
    public function getAuthCode(){
        $url="https://graph.qq.com/oauth2.0/authorize";
        $param['response_type']="code";
        $param['client_id']=$this->app_id;
        $param['redirect_uri']=$this->callBackUrl;
 
        //生成唯一随机串防CSRF攻击
        $state = md5(uniqid(rand(), TRUE));
        $_SESSION['state']=$state;
        $param['state']=$state;
        $param['scope']="get_user_info";
        $param =http_build_query($param,'','&');
        $url=$url."?".$param;
        header("Location:".$url);
    }
 
    //通过Authorization Code获取Access Token
    private function _getAccessToken(){
        $this->code=$_GET['code'];
        $url="https://graph.qq.com/oauth2.0/token";
        $param['grant_type']="authorization_code";
        $param['client_id']=$this->app_id;
        $param['client_secret']=$this->app_key;
        $param['code']=$this->code;
        $param['redirect_uri']=$this->callBackUrl;
        $param =http_build_query($param,'','&');
        $url=$url."?".$param;
        return $this->getUrl($url);
    }
 
    //获取openid
    public function _setOpenID(){
        $rzt=$this->_getAccessToken();
        parse_str($rzt,$data);
        $this->accessToken=$data['access_token'];
        $url="https://graph.qq.com/oauth2.0/me";
        $param['access_token']=$this->accessToken;
        $param =http_build_query($param,'','&');
        $url=$url."?".$param;
        $response=$this->getUrl($url);
 
        //--------检测错误是否发生
        if(strpos($response, "callback") !== false){
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos -1);
        }
        $user = json_decode($response);
 
        if(isset($user->error)){
            exit("错误代码:100007");
        }
         
        return $user->openid;
    }
 
 
    //获取信息
    public function getUserInfo(){
        if($_GET['state'] != $_SESSION['state']){
            exit("错误代码:300001");
        }
 
        $openid=$this->_setOpenID();
        if(empty($openid)){
            return false;
        }
        session('openid',$openid);
        $url="https://graph.qq.com/user/get_user_info";
        $param['access_token']=$this->accessToken;
        $param['oauth_consumer_key']=$this->app_id;
        $param['openid']=$openid;
        $param =http_build_query($param,'','&');
        $url=$url."?".$param;
        $rzt=$this->getUrl($url);
        return $rzt;
    }
 
    public function getOpenId(){
        if($_GET['state'] != $_SESSION['state']){
            exit("错误代码:300001");
        }
        $rzt=$this->_getAccessToken();
        parse_str($rzt,$data);
        $this->accessToken=$data['access_token'];
        $url="https://graph.qq.com/oauth2.0/me";
        $param['access_token']=$this->accessToken;
        $param =http_build_query($param,'','&');
        $url=$url."?".$param;
        $response=$this->getUrl($url);
 
        //--------检测错误是否发生
        if(strpos($response, "callback") !== false){
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos -1);
        }
        $info = object_array(json_decode($response));
        $qq['access_token'] = $this->accessToken;
        $qq['openid']       = $info['openid'];
        session('qq',$qq);
        return $info['openid'];
    }
 
 
    public function getInfo($openid='',$accessToken=''){
        $url="https://graph.qq.com/user/get_user_info";
        $param['oauth_consumer_key']=$this->app_id;
        $param['access_token']=$accessToken;
        $param['openid']=$openid;
        $param =http_build_query($param,'','&');
        $url=$url."?".$param;
        $rzt=$this->getUrl($url);
        $info = object_array(json_decode($rzt));
        return $info;
    }
 
 
    //CURL GET
    private function getUrl($url){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        if (!empty($options)){
            curl_setopt_array($ch, $options);
        }
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
 
 
    //CURL POST
    private function postUrl($url,$post_data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        ob_start();
        curl_exec($ch);
        $result = ob_get_contents();
        ob_end_clean();
        return $result;
    }
}
Copy after login

The following is the code in the controller

LoginController.class.php This file mainly contains two methods

The address accessed when clicking QQ login

public function qq_login(){
        $Qqconnect = new \Common\Lib\Qqconnect();
        $Qqconnect->getAuthCode();
    }
Copy after login

Callback access address

public function callback(){
        $Qqconnect = new \Common\Lib\Qqconnect();
        $openid = $Qqconnect->getOpenId();
        $qq = session('qq');
 
 
 
        $Member = M('Member');
        $map = array();
        $map['openid'] = $openid;
        $userInfo = $Member->where($map)->find();
 
 
        if(!empty($userInfo)){
            $this->success('登陆成功!',U('Member/index'));
        }else{
            $Qqconnect = new \Common\Lib\Qqconnect();
            $userInfo = $Qqconnect->getInfo($qq['openid'],$qq['access_token']);
            print_r($userInfo);
            exit;
    }
Copy after login

The above is just a simple example, you can refer to it for modification and improvement. If there is anything you don’t understand, you can leave a message for discussion.

Recommended tutorial: "PHP Video Tutorial"

The above is the detailed content of QQ third-party authentication login extension class based on Thinkphp3.2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:liqingbo.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!