首页  >  文章  >  后端开发  >  PHP怎么实现扫码登录QQ

PHP怎么实现扫码登录QQ

藏色散人
藏色散人原创
2022-11-21 09:27:181860浏览

PHP实现扫码登录QQ的方法:1、通过“public function QRcode(){...}”方法获取登录验证码;2、动态传入QQ微信的类名字符串快速实例化;3、生成二维码并轮询检测二维码状态即可。

PHP怎么实现扫码登录QQ

本教程操作环境:Windows7系统、PHP8.1版、Dell G3电脑。

PHP怎么实现扫码登录QQ?

分享一下,PHP实现第四方QQ微信扫码登陆,不接入qq互联以及微信开发者平台就可以实现用户对接鹅厂,phpQQ微信扫码登陆 

自己抓的QQ包以及整合了网上一些已经封装好了的代码

具体如下:

QQ:

get_curl_split($url);
        preg_match('/qrsig=(.*?);/',$arr['header'],$match);
        if($qrsig=$match[1])
            return array('code'=>200,'qrsig'=>$qrsig,'data'=>base64_encode($arr['body']));
        else
            return array('code'=>400,'msg'=>'二维码获取失败');
    }
    public function ListenQR($qrsig)
    {
        $qrsig = $qrsig[0];
        if(empty($qrsig))return array('code'=>-1,'msg'=>'qrsig不能为空');
        $url='https://ssl.ptlogin2.qq.com/ptqrlogin?u1=https%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&ptqrtoken='.$this->getqrtoken($qrsig).'&login_sig=&ptredirect=0&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=0-0-'.time().'0000&js_ver=10194&js_type=1&pt_uistyle=40&aid=549000912&daid=5&';
        $ret = $this->get_curl($url,0,$url,'qrsig='.$qrsig.'; ',1);
        if(preg_match("/ptuiCB\('(.*?)'\)/", $ret, $arr)){
            $r=explode("','",str_replace("', '","','",$arr[1]));
            if($r[0]==0){
                preg_match('/uin=(\d+)&/',$ret,$uin);
                $uin=$uin[1];
                preg_match('/skey=@(.{9});/',$ret,$skey);
                preg_match('/superkey=(.*?);/',$ret,$superkey);
                $data=$this->get_curl($r[2],0,0,0,1);
                if($data) {
                    preg_match("/p_skey=(.*?);/", $data, $matchs);
                    $pskey = $matchs[1];
                }
                if($pskey){
                    if(isset($_GET['findpwd'])){
                        $_SESSION['findpwd_qq']=$uin;
                    }
                    return array('code'=>200,'uin'=>$uin,'skey'=>'@'.$skey[1],'pskey'=>$pskey,'superkey'=>$superkey[1],'nick'=>$r[5]);
                }else{
                    return array('code'=>201,'msg'=>'登录成功,获取相关信息失败!'.$r[2]);
                }
            }elseif($r[0]==65){
                return array('code'=>400,'msg'=>'二维码已失效。');
            }elseif($r[0]==66){
                return array('code'=>202,'msg'=>'二维码未失效。');
            }elseif($r[0]==67){
                return array('code'=>302,'msg'=>'正在验证二维码。');
            }else{
                return array('code'=>401,'msg'=>$r[4]);
            }
        }else{
            return array('code'=>403,'msg'=>$ret);
        }
  
    }
    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;
    }
}

 

  微信:

get_curl($url);
//        var_dump($uuid);
        $uuid = substr($uuid,strpos($uuid,'"')+1,-2);
        $url = "https://login.wx.qq.com/qrcode/{$uuid}?t=webwx";
        $qrcode = file_get_contents($url);
        $result = ['code'=>200,'uuid'=>$uuid,'qrcode'=>base64_encode($qrcode)];
        return $result;
    }
    public function ListenQR($uuid)
    {
        $paras['ctime'] = 1000;
        $paras['rtime'] = 1000;
        $paras['refer'] = 'https://wx2.qq.com/';
        $api = 'https://login.wx2.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=' . $uuid[0] . '&tip=0';
        $body = $this->curl($api, $paras);
        preg_match('/(\d){3}/', $body, $code);
        preg_match('/redirect_uri="(.*?)"/', $body, $url);
        if ($code[0] == '200') {
            $body = $this->curl($url[1]);
            preg_match('/(\d*?)<\/wxuin>/', $body, $wxuin);
            $ret['code'] = 200;
            $ret['data']['uin'] = $wxuin[1];
            $ret['data']['type'] = 'wx';
            $ret['msg'] = '登录成功';
        } else {
            $ret['code'] = 408;
            $ret['msg'] = '请使用手机微信扫码登录';
        }
        return $ret;
    }
}

  为了方便跳用,这里我又封装了一个类

动态传入QQ微信的类名字符串快速实例化

Tencent类:

cl = new $type();
    }
    public function Psr4Autoload($class)
    {
    $class_file = $this->path .'/'. $class . '.php';
    if (file_exists($class_file))
    {
        include "$class_file";
    }else{
        die('类文件'.$class_file .'不存在');
    }
    }
    public function QRcode()
    {
        return call_user_func([$this->cl,__FUNCTION__]);
    }
    public function ListenQR(...$args)
    {
        return call_user_func([$this->cl,__FUNCTION__],$args);
    }
    public function __call($name, $arguments)
    {
       call_user_func_array([$this->cl,$name],(array)$arguments);
    }
}

  以及最后一个curl类:

ua);
        }
        if($nobaody){
            curl_setopt($ch, CURLOPT_NOBODY,1);
  
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }
    function curl($url, $paras = array()) {
        $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 ($paras['ctime']) { // 连接超时
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $paras['ctime']);
        }
        if ($paras['rtime']) { // 读取超时
            curl_setopt($ch, CURLOPT_TIMEOUT_MS, $paras['rtime']);
        }
        if ($paras['post']) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
        }
        if ($paras['header']) {
            curl_setopt($ch, CURLOPT_HEADER, true);
        }
        if ($paras['cookie']) {
            curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']);
        }
        if ($paras['refer']) {
            if ($paras['refer'] == 1) {
                curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
            } else {
                curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
            }
        }
        if ($paras['ua']) {
            curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']);
        } else {
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
        }
        if ($paras['nobody']) {
            curl_setopt($ch, CURLOPT_NOBODY, 1);
        }
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }
     public function get_curl_split($url){
        $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: keep-alive";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT,$this->ua);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $ret = curl_exec($ch);
        $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;
    }
}

  大致调用的流程

1、保存四个类到文件里面

2、引入文件

3、单独写两个接口,一个生成qr码(base64),一个轮询二维码扫码状态

4、用户扫码成功后、会返回一个QQ号火微信唯一id

5、这里我只演示一个微信扫码登陆的例子

生成二维码并轮询检测二维码状态 login.php :

QRcode();
?>


  

  ajax.php :

ListenQR($_GET['uuid']);
//var_dump($ret);
echo json_encode($ret,true);exit;

  这个例子是微信的,QQ同样的代码一样可以运行

数据库用户表多一个qq和wxuin字段、用于保存用户绑定的QQ和微信

上面那个仅仅只是个例子,可能写的不是很好。大佬勿喷

推荐学习:《PHP视频教程

以上是PHP怎么实现扫码登录QQ的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn