首页 > php框架 > ThinkPHP > 以Thinkphp3.2为基础的QQ第三方认证登录扩展类

以Thinkphp3.2为基础的QQ第三方认证登录扩展类

coldplay.xixi
发布: 2020-06-09 12:04:11
转载
3044 人浏览过

以Thinkphp3.2为基础的QQ第三方认证登录扩展类

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

基于Thinkphp3.2的qq第三方认证登录扩展类,其实以下类也是我从TP官网收集整理得来的,稍微做了一下修改和完善。

这里我将文件放存在“/Application/Common/Lib/Qqconnect.class.php”。(其实这个文件路径根据自己喜好来放)

实例化

$Qqconnect = new \Common\Lib\Qqconnect();
登录后复制

在__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。

Qqconnect.class.php

<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015.
// +----------------------------------------------------------------------
// | Author: qiandutianxia <852997402@qq.com>
// +----------------------------------------------------------------------
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[&#39;QC_userData&#39;])){
            self::$data = array();
        }else{
            self::$data = $_SESSION[&#39;QC_userData&#39;];
        }
    }
 
 
    //获取Authorization Code
    public function getAuthCode(){
        $url="https://graph.qq.com/oauth2.0/authorize";
        $param[&#39;response_type&#39;]="code";
        $param[&#39;client_id&#39;]=$this->app_id;
        $param[&#39;redirect_uri&#39;]=$this->callBackUrl;
 
        //生成唯一随机串防CSRF攻击
        $state = md5(uniqid(rand(), TRUE));
        $_SESSION[&#39;state&#39;]=$state;
        $param[&#39;state&#39;]=$state;
        $param[&#39;scope&#39;]="get_user_info";
        $param =http_build_query($param,&#39;&#39;,&#39;&&#39;);
        $url=$url."?".$param;
        header("Location:".$url);
    }
 
    //通过Authorization Code获取Access Token
    private function _getAccessToken(){
        $this->code=$_GET[&#39;code&#39;];
        $url="https://graph.qq.com/oauth2.0/token";
        $param[&#39;grant_type&#39;]="authorization_code";
        $param[&#39;client_id&#39;]=$this->app_id;
        $param[&#39;client_secret&#39;]=$this->app_key;
        $param[&#39;code&#39;]=$this->code;
        $param[&#39;redirect_uri&#39;]=$this->callBackUrl;
        $param =http_build_query($param,&#39;&#39;,&#39;&&#39;);
        $url=$url."?".$param;
        return $this->getUrl($url);
    }
 
    //获取openid
    public function _setOpenID(){
        $rzt=$this->_getAccessToken();
        parse_str($rzt,$data);
        $this->accessToken=$data[&#39;access_token&#39;];
        $url="https://graph.qq.com/oauth2.0/me";
        $param[&#39;access_token&#39;]=$this->accessToken;
        $param =http_build_query($param,&#39;&#39;,&#39;&&#39;);
        $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[&#39;state&#39;] != $_SESSION[&#39;state&#39;]){
            exit("错误代码:300001");
        }
 
        $openid=$this->_setOpenID();
        if(empty($openid)){
            return false;
        }
        session(&#39;openid&#39;,$openid);
        $url="https://graph.qq.com/user/get_user_info";
        $param[&#39;access_token&#39;]=$this->accessToken;
        $param[&#39;oauth_consumer_key&#39;]=$this->app_id;
        $param[&#39;openid&#39;]=$openid;
        $param =http_build_query($param,&#39;&#39;,&#39;&&#39;);
        $url=$url."?".$param;
        $rzt=$this->getUrl($url);
        return $rzt;
    }
 
    public function getOpenId(){
        if($_GET[&#39;state&#39;] != $_SESSION[&#39;state&#39;]){
            exit("错误代码:300001");
        }
        $rzt=$this->_getAccessToken();
        parse_str($rzt,$data);
        $this->accessToken=$data[&#39;access_token&#39;];
        $url="https://graph.qq.com/oauth2.0/me";
        $param[&#39;access_token&#39;]=$this->accessToken;
        $param =http_build_query($param,&#39;&#39;,&#39;&&#39;);
        $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[&#39;access_token&#39;] = $this->accessToken;
        $qq[&#39;openid&#39;]       = $info[&#39;openid&#39;];
        session(&#39;qq&#39;,$qq);
        return $info[&#39;openid&#39;];
    }
 
 
    public function getInfo($openid=&#39;&#39;,$accessToken=&#39;&#39;){
        $url="https://graph.qq.com/user/get_user_info";
        $param[&#39;oauth_consumer_key&#39;]=$this->app_id;
        $param[&#39;access_token&#39;]=$accessToken;
        $param[&#39;openid&#39;]=$openid;
        $param =http_build_query($param,&#39;&#39;,&#39;&&#39;);
        $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;
    }
}
登录后复制

下面是控制器中的代码

LoginController.class.php该文件主要包含两个方式

点击QQ登陆的时候访问的地址

public function qq_login(){
        $Qqconnect = new \Common\Lib\Qqconnect();
        $Qqconnect->getAuthCode();
    }
登录后复制

   回调访问地址

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

以上只是简单的示例,大家可以参考一下再进行修改和完善。如有不明白的地方可以留言讨论。

推荐教程:《PHP视频教程

以上是以Thinkphp3.2为基础的QQ第三方认证登录扩展类的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:liqingbo.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板