24 06 2019
网上找了一堆的TP5/5.1关于QQ第三方登陆的,都是直接用的QQ互联的登陆包做的改造,感觉还不如Tp3以前的QQconnect.class.php好用,于是想着改造了一下以前QQconnect.class.php.不多说了,开始正题

第一步,在自己的项目文件中,新建一个common,如下图,这是我之前建好的,将改造后的QQconnect.php文件放入了Third文件下
第二步,加入命名空间
namespace app\common\Third;
第三步,加入需要用到的一些Think内置功能
use think\facade\Session;

我这里只用到了Session;(改造了以前的$_SESSION),将之前的$_Session换成Session::set()赋值,Session::get()取值

第四步,修改里面的$_GET()为input()方式接收
第五步,新增一个get请求方法作为内置方法
//发送get请求
    private function get($url, $param = null) {
        if($param != null) {
            $query = http_build_query($param);
            $url = $url . '?' . $query;
        }
        $ch = curl_init();
        if(stripos($url, "https://") !== false){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        $content = curl_exec($ch);
        $status = curl_getinfo($ch);
        curl_close($ch);
        if(intval($status["http_code"]) == 200) {
            return $content;
        }else{
            echo $status["http_code"];
            return false;
        }
    }
第六步,将QQ互联类里面的get方法全部换成$this->get方法进行即可.

最后说明一下,为了符合TP5.1的规则,才不得已而为之,最后调用的时候

从前端调用的地址如下,入口地址 我这里是http://yuming/(这里隐藏了index.php)auth/third/type/qq.html

重点:回调地址我这里是http://yuming/auth/index/type/qq.html

QQconnect具体代码如下

<?php
namespace app\common\Third;
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://xf.fengzhiya.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: @Xf
// +----------------------------------------------------------------------
use think\facade\Session;
class QQConnect {
    //发送get请求
    private function get($url, $param = null) {
        if($param != null) {
            $query = http_build_query($param);
            $url = $url . '?' . $query;
        }
        $ch = curl_init();
        if(stripos($url, "https://") !== false){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        $content = curl_exec($ch);
        $status = curl_getinfo($ch);
        curl_close($ch);
        if(intval($status["http_code"]) == 200) {
            return $content;
        }else{
            echo $status["http_code"];
            return false;
        }
    }
    //获取accesstoken
    private function get_access_token($appkey, $appsecretkey, $code, $callback, $state) {
        if($state == Session::get('state')) {
            $url = "https://graph.qq.com/oauth2.0/token";
            $param = array(
                "grant_type"    =>    "authorization_code",
                "client_id"     =>    $appkey,
                "client_secret" =>    $appsecretkey,
                "code"          =>    $code,
                "state"         =>    $state,
                "redirect_uri"  =>    $callback
            );
            $response = $this->get($url, $param);
            if($response == false) {
                return false;
            }
            $params = array();
            parse_str($response, $params);
            return $params["access_token"];
        } else {
            exit("The state does not match. You may be a victim of CSRF.");
        }
    }
   //获取openid
    private function get_openid($access_token) {
        $url = "https://graph.qq.com/oauth2.0/me"; 
        $param = array(
            "access_token"    => $access_token
        );

        $response  = $this->get($url, $param);
        if($response == false) {
            return false;
        }
        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) || $user->openid == "") {
            return false;
        }
        return $user->openid;
    }
    //获取用户信息
    public function get_user_info($token, $openid, $appkey, $format = "json") {
        $url = "https://graph.qq.com/user/get_user_info";
        $param = array(
            "access_token"      =>    $token,
            "oauth_consumer_key"=>    $appkey,
            "openid"            =>    $openid,
            "format"            =>    $format
        );

        $response = $this->get($url, $param);
        if($response == false) {
            return false;
        }

        $user = json_decode($response, true);
        return $user;
    }
   //从前端请求后直接请求到此处开始执行
    public function login($appkey, $callback, $scope='') {
        Session::set('state',md5(uniqid(rand(), TRUE))); //CSRF protection
        $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
            . $appkey . "&redirect_uri=" . urlencode($callback)
            . "&state=" . Session::get('state')
            . "&scope=".$scope;
        header('Location:'.$login_url);
    }
    //此处开始回调
    public function callback($appkey, $appsecretkey, $callback) {
        $code = input('code');
        $state = Session::get('state');

        $token = $this->get_access_token($appkey, $appsecretkey, $code, $callback, $state);
        $openid = $this->get_openid($token);
        if(!$token || !$openid) {
            exit('get token or openid error!');
        }

        return array('openid' => $openid, 'token' => $token);
    }
}

整体基本完成了,希望能帮上大家,kiss.

下一期分享新浪第三方登陆的tp5.1类

延伸阅读
  1. 风之涯Chat 聊天群tp5.1实现 包含workerman实现WebSocket[2019-10-19 16:28:39]
  2. TP5.1实现邮件发送功能[简单][2019-07-16 19:40:01]
  3. PHP读取doc-docx-xls-pdf-txt内容[2019-07-12 22:18:37]
  4. TP5.1整合微博第三方登陆类[概要],与上期的QQ登陆差不多[2019-06-25 22:57:03]
  5. ThinkPHP5.1你一定要引起重视的几个细节[2019-06-23 13:42:09]
  6. PHP生成随机数的几种方法[2019-06-22 02:43:46]
发表评论