借鉴了彩虹,云青大佬的代码研究了一下做出来了自己的QQ扫码登陆并接入到网站话不多说

请输入图片描述

php代码

  // 生成二维码
    public function getqrpic()
    {
        $url = 'https://ssl.ptlogin2.qq.com/ptqrshow?appid=1006102&e=2&l=M&s=4&d=72&v=4&t=0.5409099'.time() .'daid=1&pt_3rd_aid=0';
        $arr = $this->get_curl($url, 0, 0, 0, 1, 0, 0, 1);
        preg_match('/qrsig=(.*?);/', $arr['header'], $match);
        if ($qrsig = $match[1]) {
            exit('{"code":0,"qrsig":"'.$qrsig.'","data":"'.base64_encode($arr['body']).'"}');
        } else {
            exit('{"code":1,"msg":"二维码获取失败"}');
        }
    }
    //转换token
    private function getqrtoken($qrsig){
        $len = strlen($qrsig);
        $hash = 0;
        for($i = 0; $i < $len; $i++){
            $hash += (($hash << 5) & 2147483647) + ord($qrsig[$i]) & 2147483647;
            $hash &= 2147483647;
        }
        return $hash & 2147483647;
    }
    // 获取二维码状态
    public function qqlogin()
    {
        error_reporting(0);
        $qrsig=empty($_GET['qrsig'])?exit('{"code:400,"msg":"qrsig不能为空"}'):$_GET['qrsig'];
        $sig = '';
        $url = 'https://ssl.ptlogin2.qq.com/ptqrlogin?u1=https%3A%2F%2Fid.qq.com%2Findex.html&ptqrtoken='.$this->getqrtoken($qrsig).'&ptredirect=1&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=0-0-' . time() . '0000&js_ver=10291&js_type=1&login_sig=' . $sig . '&pt_uistyle=40&aid=1006102&daid=1&';
        $ret = $this->get_curl($url, 0, $url, 'qrsig=' . $qrsig . '; ', 1);
        preg_match("/ptuiCB\('(.*?)'\)/", $ret, $ts_arr);
        $r = @explode("','", str_replace("', '", "','", $ts_arr[1]));
        if ($r[0] == 65) {
            echo json_encode(['code' => 200, 'msg' => '二维码已失效']);
        } elseif ($r[0] == 66) {
            echo json_encode(['code' => 202, 'msg' => '二维码未失效']);
        } elseif ($r[0] == 67) {
            echo json_encode(['code' => 204, 'msg' => '正在验证二维码']);
        } else {
            if (strpos($ret, '403')){
                $result = array('state'=>'306','message' =>$ret);
                echo json_encode($result);
            }
        }
        if (strlen($ret) > 1000) {
            $arr = explode(';', $ret);
            $data = substr($arr[17], strpos($arr[17], '=o') + 2, strlen($arr[17]));
            if (empty($data)) {
                echo json_encode(['code' => 400, 'msg' => '请切换二维码'.$ret]);
            }
            $qq = ltrim($data, '0');
            $name = ltrim($data, '');
            // $qq是qq号,下面写你接下来需要处理的语句
            var_dump($ret);
            echo $qq;
        }
    }
    //获取官方接口
    private function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0,$split=0){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $httpheader[] = "Accept:*/*";
        $httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
        $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
        $httpheader[] = "Connection:close";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
        if($post){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        if($header){
            curl_setopt($ch, CURLOPT_HEADER, TRUE);
        }
        if($cookie){
            curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        }
        if($referer){
            curl_setopt($ch, CURLOPT_REFERER, $referer);
        }
        if($ua){
            curl_setopt($ch, CURLOPT_USERAGENT,$ua);
        }else{
            curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36');
        }
        if($nobaody){
            curl_setopt($ch, CURLOPT_NOBODY,1);

        }
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $ret = curl_exec($ch);
        if ($split) {
            $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $header = substr($ret, 0, $headerSize);
            $body = substr($ret, $headerSize);
            $ret=array();
            $ret['header']=$header;
            $ret['body']=$body;
        }
        curl_close($ch);
        return $ret;
    }

js代码

<script>
    getqrpic();
    function getqrpic() {
        layer.msg('加载中~~~~');
        var getvcurl = "";
        $.get(getvcurl, function (res) {
            console.log(res.qrsig)
            if (res.code == 0) {
                $('#qrimg').attr('qrsig', res.qrsig);
                $('#qrimg').html('<img onclick="getqrpic()" src="data:image/png;base64,' + res.data + '" title="点击刷新">');
            } else {
                //alert(res.msg);
            }
        }, 'json');
    }

    function getMsg() {
        var qrsig=$('#qrimg').attr('qrsig');
        var getvcurl = "" + decodeURIComponent(qrsig);
        $.get(getvcurl, function (res) {
            if (res.code == 200) {
                $.post("",
                    {
                        msg:res.msg,
                        qq:res.qq,
                    },
                    function(data){
                        if (data.msg=="登陆成功"){
                            var index = layer.load(0, {shade: false});
                            layer.alert("登陆成功!!")
                            window.location.href='/index/users/index';
                        }else {
                            var index = layer.load(0, {shade: false});
                            layer.alert("QQ不存在!!!")
                            window.location.href='';
                        }
                    });
            } else if (res.code == 205 || res.code == 306) {
                getqrpic();
            } else {

            }
        }, 'json');
    }
    $(document).ready(function(){
        interval1=setInterval(getMsg,2000);
    });
</script>

原理就是获取官方的qrsig用官方接口获取二维码通过前端定时器刷新以及检测二维码状态
demo:http://tp.79xj.cn/index/index/qq_login.html

Last modification:November 16th, 2019 at 01:24 pm
如果觉得我的文章对你有用,请随意赞赏