Home Backend Development PHP Problem How to use PHP to scan QR code to log in to QQ

How to use PHP to scan QR code to log in to QQ

Nov 21, 2022 am 09:27 AM
php qq

PHP method to implement scanning code to log in to QQ: 1. Obtain the login verification code through the "public function QRcode(){...}" method; 2. Dynamically pass in the class name string of QQ WeChat for a quick example 3. Generate the QR code and poll to detect the QR code status.

How to use PHP to scan QR code to log in to QQ

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to scan QR code to log in to QQ with PHP?

Share, PHP realizes the fourth-party QQ WeChat scan code login, users can connect to Goose Factory without accessing QQ Internet and WeChat developer platform, phpQQ WeChat scan code login

The QQ package I grabbed and integrated some packaged codes on the Internet

The details are as follows:

QQ:

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

WeChat:

<?php
class Wechat extends Curl_Api
{
    //获取验证码
    public function QRcode()
    {
        $url = "https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&fun=new&lang=zh_CN";
        $uuid = $this->get_curl($url);
//        var_dump($uuid);
        $uuid = substr($uuid,strpos($uuid,&#39;"&#39;)+1,-2);
        $url = "https://login.wx.qq.com/qrcode/{$uuid}?t=webwx";
        $qrcode = file_get_contents($url);
        $result = [&#39;code&#39;=>200,&#39;uuid&#39;=>$uuid,&#39;qrcode&#39;=>base64_encode($qrcode)];
        return $result;
    }
    public function ListenQR($uuid)
    {
        $paras[&#39;ctime&#39;] = 1000;
        $paras[&#39;rtime&#39;] = 1000;
        $paras[&#39;refer&#39;] = &#39;https://wx2.qq.com/&#39;;
        $api = &#39;https://login.wx2.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=&#39; . $uuid[0] . &#39;&tip=0&#39;;
        $body = $this->curl($api, $paras);
        preg_match(&#39;/(\d){3}/&#39;, $body, $code);
        preg_match(&#39;/redirect_uri="(.*?)"/&#39;, $body, $url);
        if ($code[0] == &#39;200&#39;) {
            $body = $this->curl($url[1]);
            preg_match(&#39;/<wxuin>(\d*?)<\/wxuin>/&#39;, $body, $wxuin);
            $ret[&#39;code&#39;] = 200;
            $ret[&#39;data&#39;][&#39;uin&#39;] = $wxuin[1];
            $ret[&#39;data&#39;][&#39;type&#39;] = &#39;wx&#39;;
            $ret[&#39;msg&#39;] = &#39;登录成功&#39;;
        } else {
            $ret[&#39;code&#39;] = 408;
            $ret[&#39;msg&#39;] = &#39;请使用手机微信扫码登录&#39;;
        }
        return $ret;
    }
}
Copy after login

In order to facilitate the use, here I have encapsulated a class

The class name string dynamically passed into QQ WeChat is quickly instantiated

Tencent class:

<?php
Class Tencent{
    protected $path = __DIR__ . &#39;/&#39;;
     private $cl;
     /*
      * 动态传入QQ或WX字符串,自动转换对应的api类登录
      */
    public function __construct($type)
    {
        //注册自动加载函数
        spl_autoload_register([$this,&#39;Psr4Autoload&#39;]);
        //引入curl
        $this->cl = new $type();
    }
    public function Psr4Autoload($class)
    {
    $class_file = $this->path .&#39;/&#39;. $class . &#39;.php&#39;;
    if (file_exists($class_file))
    {
        include "$class_file";
    }else{
        die(&#39;类文件&#39;.$class_file .&#39;不存在&#39;);
    }
    }
    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);
    }
}
Copy after login

And the last curl class:

<?php
class Curl_Api
{
    public $ua = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36";
      
     public function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=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: application/json";
        $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);
        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,$this->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[&#39;ctime&#39;]) { // 连接超时
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $paras[&#39;ctime&#39;]);
        }
        if ($paras[&#39;rtime&#39;]) { // 读取超时
            curl_setopt($ch, CURLOPT_TIMEOUT_MS, $paras[&#39;rtime&#39;]);
        }
        if ($paras[&#39;post&#39;]) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $paras[&#39;post&#39;]);
        }
        if ($paras[&#39;header&#39;]) {
            curl_setopt($ch, CURLOPT_HEADER, true);
        }
        if ($paras[&#39;cookie&#39;]) {
            curl_setopt($ch, CURLOPT_COOKIE, $paras[&#39;cookie&#39;]);
        }
        if ($paras[&#39;refer&#39;]) {
            if ($paras[&#39;refer&#39;] == 1) {
                curl_setopt($ch, CURLOPT_REFERER, &#39;http://m.qzone.com/infocenter?g_f=&#39;);
            } else {
                curl_setopt($ch, CURLOPT_REFERER, $paras[&#39;refer&#39;]);
            }
        }
        if ($paras[&#39;ua&#39;]) {
            curl_setopt($ch, CURLOPT_USERAGENT, $paras[&#39;ua&#39;]);
        } 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[&#39;nobody&#39;]) {
            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[&#39;header&#39;]=$header;
        $ret[&#39;body&#39;]=$body;
        curl_close($ch);
        return $ret;
    }
}
Copy after login

Rough calling process

1. Save four classes to files

2. Import the file

3. Write two separate interfaces, one to generate QR code (base64), and one to poll the QR code scanning status

4. After the user successfully scans the code, a QQ Huo WeChat unique id

5. Here I only demonstrate an example of scanning the WeChat code to log in

Generate QR code and poll to detect the QR code status login.php:

<?php
//我这里只引入了一个文件的原因是因为Wechat和QQ类不用引入、只需要把Curl_Api请求类引入进来就好,但我Tencent类内已经引入了。所以这里我只需要引入一个文件就好
include "Lib/Tencent/Tencent.php";
$wx = new Tencent("Wechat");
$ret = $wx->QRcode();
?>
<!--直接生成QR码、记得把uuid给带上-->
<img id="wx" src="data:text/html;base64,<?=$ret[&#39;qrcode&#39;]?>" uuid="<?=$ret[&#39;uuid&#39;]?>">
  
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script>
    // setTimeout(function () {
    //     var uuid = document.getElementById(&#39;wx&#39;).getAttribute("uuid");
    //     var url ="/ajax.php?uuid="+uuid;
    //     console.log(url);
    // },1000);
    //每秒去查询一次二维码状态
    $(document).ready(function () {
        setInterval(function () {
            var uuid = document.getElementById(&#39;wx&#39;).getAttribute("uuid");
            var url ="/ajax.php?uuid="+uuid;
            $.ajax({type:"GET",url:url,success:function (data) {
                    if (data.code == 200)
                    {
                        alert("登陆成功,uin为:"+data.uid);
                    }
                }});
        },1000);
    });
  
</script>
Copy after login

 ajax.php:

<?php
include "Lib/Tencent/Tencent.php";
$wx = new Tencent("Wechat");
//直接获取到uuid后,监听就好了
$ret = $wx->ListenQR($_GET[&#39;uuid&#39;]);
//var_dump($ret);
echo json_encode($ret,true);exit;
Copy after login

 This example is for WeChat, the same code can be run on QQ

The database user table has one more qq and wxuin fields for saving user bindings QQ and WeChat

The above is just an example, and may not be written very well. Boss, don’t spray

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to use PHP to scan QR code to log in to QQ. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What software can make Bitcoin? Top 10 Bitcoin Trading Software Recommendations in 2025 What software can make Bitcoin? Top 10 Bitcoin Trading Software Recommendations in 2025 Feb 21, 2025 pm 09:30 PM

With the rapid development of the Bitcoin market, it is crucial to choose reliable trading software. This article will recommend the top ten Bitcoin trading software in 2025 to help you trade efficiently and safely. These software have been rigorously screened and consider factors such as functionality, security, user-friendliness and support levels. From beginner-friendly platforms to complex tools for experienced traders, you will find the best options for your trading needs in this list.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles