PHP实现QQ扫码登入,非开放平台

白鸽
2020-04-15 / 0 评论 / 295 阅读 / 正在检测是否收录...

原理就是生成二维码后,前端设置定时器查看二维码状态,因为没有openid只能保存固定的值,那就是qq号了

<!--more-->

        // 生成二维码
    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]) {
            Json::msg(0, 'success', ['qrsig' => $qrsig, 'data' => base64_encode($arr['body'])]);
        } else {
           Json::msg(1, '二维码获取失败');
       }
    }

    // 获取二维码状态
    public function qqlogin()
    {
        error_reporting(0);
        if (input('?get.qrsig')) {
            $qrsig = input('get.qrsig');
        } else {
            Json::msg(400, '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) {
            Json::msg(205, '二维码已失效');
        } elseif ($r[0] == 66) {
            Json::msg(202, '二维码未失效');
        } elseif ($r[0] == 67) {
        Json::msg(204, '正在验证二维码');
        } else {
            if (strpos($ret, '403')) Json::msg(306, $ret);
        }
        if (strlen($ret) > 1000) {
            $arr = explode(';', $ret);
            $data = substr($arr[17], strpos($arr[17], '=o') + 2, strlen($arr[17]));
            if (empty($data)) {
                Json::msg(400, '请切换二维码' . $ret);
            }
            $qq = ltrim($data, '0');
            // $qq是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);
        $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


        getqrpic();
    function getqrpic() {
      var getvcurl = "qlogin/getqrpic/r/" + Math.random(1);
        $.get(getvcurl, function (res) {
              console.log(res.info.qrsig)
            if (res.status == 0) {
                $('#qrimg').attr('qrsig', res.info.qrsig);
                $('#qrimg').html('<img onclick="getqrpic()" src="data:image/png;base64,' + res.info.data + '" title="点击刷新">');
            } else {
                //alert(res.msg);
            }
        }, 'json');
    }

      $(document).ready(function(){
        interval1=setInterval(getMsg,2000);
    });

      function getMsg() {
          var qrsig=$('#qrimg').attr('qrsig');
        var getvcurl = "qlogin/qqlogin/qrsig/" + decodeURIComponent(qrsig) + '/r/' + Math.random(1);
        $.get(getvcurl, function (res) {
            if (res.status == 200) {
                // 这里写登入过后你执行的语句
            } else if (res.status == 205 || res.status == 306) {
                getqrpic();
            } else {
            }
        }, 'json');
    }
0

评论 (0)

取消