PHP實作掃碼登入QQ的方法:1、透過「public function QRcode(){...}」方法取得登入驗證碼;2、動態傳入QQ微信的類別名稱字串快速實例化;3、產生二維碼並輪詢檢測二維碼狀態即可。
本教學操作環境:Windows7系統、PHP8.1版、Dell G3電腦。
PHP怎麼實作掃碼登入QQ?
分享,PHP實現第四方QQ微信掃碼登陸,不接入qq互聯以及微信開發者平台就可以實現用戶對接鵝廠,phpQQ微信掃碼登陸
自己抓的QQ套件以及整合了網路上一些已經封裝好了的程式碼
如下:
QQ:
<?php class QQ extends Curl_Api { //获取登录验证码 public function QRcode() { $url='https://ssl.ptlogin2.qq.com/ptqrshow?appid=549000912&e=2&l=M&s=4&d=72&v=4&t=0.5409099'.time().'&daid=5'; $arr=$this->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; } }
微信:
<?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,'"')+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('/<wxuin>(\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類別:
<?php Class Tencent{ protected $path = __DIR__ . '/'; private $cl; /* * 动态传入QQ或WX字符串,自动转换对应的api类登录 */ public function __construct($type) { //注册自动加载函数 spl_autoload_register([$this,'Psr4Autoload']); //引入curl $this->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類別:
<?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['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 :
<?php //我这里只引入了一个文件的原因是因为Wechat和QQ类不用引入、只需要把Curl_Api请求类引入进来就好,但我Tencent类内已经引入了。所以这里我只需要引入一个文件就好 include "Lib/Tencent/Tencent.php"; $wx = new Tencent("Wechat"); $ret = $wx->QRcode(); ?> <!--直接生成QR码、记得把uuid给带上--> <img src="/static/imghwm/default1.png" data-src="data:text/html;base64,<?=$ret['qrcode']? alt="PHP怎麼實作掃碼登入QQ" >" class="lazy" id="wx" uuid="<?=$ret['uuid']?>"> <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script> <script> // setTimeout(function () { // var uuid = document.getElementById('wx').getAttribute("uuid"); // var url ="/ajax.php?uuid="+uuid; // console.log(url); // },1000); //每秒去查询一次二维码状态 $(document).ready(function () { setInterval(function () { var uuid = document.getElementById('wx').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>
ajax.php :
<?php include "Lib/Tencent/Tencent.php"; $wx = new Tencent("Wechat"); //直接获取到uuid后,监听就好了 $ret = $wx->ListenQR($_GET['uuid']); //var_dump($ret); echo json_encode($ret,true);exit;
這個例子是微信的,QQ同樣的程式碼一樣可以運行
資料庫用戶表多一個qq和wxuin字段、用於保存用戶綁定的QQ和微信
上面那個只是只是個例子,可能寫的不是很好。大佬勿噴
推薦學習:《PHP影片教學》
以上是PHP怎麼實作掃碼登入QQ的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文比較了酸和基本數據庫模型,詳細介紹了它們的特徵和適當的用例。酸優先確定數據完整性和一致性,適合財務和電子商務應用程序,而基礎則側重於可用性和

本文討論了確保PHP文件上傳的確保,以防止諸如代碼注入之類的漏洞。它專注於文件類型驗證,安全存儲和錯誤處理以增強應用程序安全性。

本文討論了在PHP中實施API速率限制的策略,包括諸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之類的庫。它還涵蓋監視,動態調整速率限制和手

本文討論了使用password_hash和pyspasswify在PHP中使用密碼的好處。主要論點是,這些功能通過自動鹽,強大的哈希算法和SECH來增強密碼保護

本文討論了OWASP在PHP和緩解策略中的十大漏洞。關鍵問題包括注射,驗證損壞和XSS,並提供用於監視和保護PHP應用程序的推薦工具。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3漢化版
中文版,非常好用