当前位置: 无忧屋首页 > 文章中心 > PHP >

PHP绕过QQ微信开发平台 第四方扫码登陆功能开发

来源:网络

发布人:天道酬勤

发布时间:2024-02-26

无法备案的网站就没办法使用QQ微信快捷登录了,所以在网上找了下第四方的QQ微信登录方法。这篇分享给大家。

目前也就整合了下QQ微信的扫码登陆,自己抓的QQ包以及整合了网上一些已经封装好了的代码。

QQ扫码登录

  1. <?php
  2. class QQ extends Curl_Api
  3. {
  4. //获取登录验证码
  5. public function QRcode()
  6. {
  7. $url='https://ssl.ptlogin2.qq.com/ptqrshow?appid=549000912&amp;e=2&amp;l=M&amp;s=4&amp;d=72&amp;v=4&amp;t=0.5409099'.time().'&amp;daid=5';
  8. $arr=$this->get_curl_split($url);
  9. preg_match('/qrsig=(.*?);/',$arr['header'],$match);
  10. if($qrsig=$match[1])
  11. return array('code'=>200,'qrsig'=>$qrsig,'data'=>base64_encode($arr['body']));
  12. else
  13. return array('code'=>400,'msg'=>'二维码获取失败');
  14. }
  15. public function ListenQR($qrsig)
  16. {
  17. $qrsig = $qrsig[0];
  18. if(empty($qrsig))return array('code'=>-1,'msg'=>'qrsig不能为空');
  19. $url='https://ssl.ptlogin2.qq.com/ptqrlogin?u1=https%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&amp;ptqrtoken='.$this->getqrtoken($qrsig).'&amp;login_sig=&amp;ptredirect=0&amp;h=1&amp;t=1&amp;g=1&amp;from_ui=1&amp;ptlang=2052&amp;action=0-0-'.time().'0000&amp;js_ver=10194&amp;js_type=1&amp;pt_uistyle=40&amp;aid=549000912&amp;daid=5&amp;';
  20. $ret = $this->get_curl($url,0,$url,'qrsig='.$qrsig.'; ',1);
  21. if(preg_match("/ptuiCB('(.*?)')/", $ret, $arr)){
  22. $r=explode("','",str_replace("', '","','",$arr[1]));
  23. if($r[0]==0){
  24. preg_match('/uin=(d+)&amp;/',$ret,$uin);
  25. $uin=$uin[1];
  26. preg_match('/skey=@(.{9});/',$ret,$skey);
  27. preg_match('/superkey=(.*?);/',$ret,$superkey);
  28. $data=$this->get_curl($r[2],0,0,0,1);
  29. if($data) {
  30. preg_match("/p_skey=(.*?);/", $data, $matchs);
  31. $pskey = $matchs[1];
  32. }
  33. if($pskey){
  34. if(isset($_GET['findpwd'])){
  35. $_SESSION['findpwd_qq']=$uin;
  36. }
  37. return array('code'=>200,'uin'=>$uin,'skey'=>'@'.$skey[1],'pskey'=>$pskey,'superkey'=>$superkey[1],'nick'=>$r[5]);
  38. }else{
  39. return array('code'=>201,'msg'=>'登录成功,获取相关信息失败!'.$r[2]);
  40. }
  41. }elseif($r[0]==65){
  42. return array('code'=>400,'msg'=>'二维码已失效。');
  43. }elseif($r[0]==66){
  44. return array('code'=>202,'msg'=>'二维码未失效。');
  45. }elseif($r[0]==67){
  46. return array('code'=>302,'msg'=>'正在验证二维码。');
  47. }else{
  48. return array('code'=>401,'msg'=>$r[4]);
  49. }
  50. }else{
  51. return array('code'=>403,'msg'=>$ret);
  52. }
  53. }
  54. private function getqrtoken($qrsig){
  55. $len = strlen($qrsig);
  56. $hash = 0;
  57. for($i = 0; $i < $len; $i++){
  58. $hash += (($hash << 5) &amp; 2147483647) + ord($qrsig[$i]) &amp; 2147483647;
  59. $hash &amp;= 2147483647;
  60. }
  61. return $hash &amp; 2147483647;
  62. }
  63. }
微信扫码登录
  1. <?php
  2. class Wechat extends Curl_Api
  3. {
  4. //获取验证码
  5. public function QRcode()
  6. {
  7. $url = "https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&amp;fun=new&amp;lang=zh_CN";
  8. $uuid = $this->get_curl($url);
  9. //        var_dump($uuid);
  10. $uuid = substr($uuid,strpos($uuid,'"')+1,-2);
  11. $url = "https://login.wx.qq.com/qrcode/{$uuid}?t=webwx";
  12. $qrcode = file_get_contents($url);
  13. $result = ['code'=>200,'uuid'=>$uuid,'qrcode'=>base64_encode($qrcode)];
  14. return $result;
  15. }
  16. public function ListenQR($uuid)
  17. {
  18. $paras['ctime'] = 1000;
  19. $paras['rtime'] = 1000;
  20. $paras['refer'] = 'https://wx2.qq.com/';
  21. $api = 'https://login.wx2.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&amp;uuid=' . $uuid[0] . '&amp;tip=0';
  22. $body = $this->curl($api, $paras);
  23. preg_match('/(d){3}/', $body, $code);
  24. preg_match('/redirect_uri="(.*?)"/', $body, $url);
  25. if ($code[0] == '200') {
  26. $body = $this->curl($url[1]);
  27. if (strpos($body,'1203'))
  28. {
  29. $ret['code'] = 400;
  30. $ret['msg'] = "短时间内不能重复进行微信登陆";
  31. return $ret;
  32. }   
  33. preg_match('/<wxuin>(d*?)</wxuin>/', $body, $wxuin);
  34. $ret['code'] = 200;
  35. $ret['data']['uin'] = $wxuin[1];
  36. $ret['data']['type'] = 'wx';
  37. $ret['msg'] = '登录成功';
  38. } else {
  39. $ret['code'] = 408;
  40. $ret['msg'] = '请使用手机微信扫码登录';
  41. }
  42. return $ret;
  43. }
  44. }
为了方便跳用,这里我又封装了一个类
动态传入QQ微信的类名字符串快速实例化

Tencent类
  1. <?php
  2. Class Tencent{
  3. protected $path = __DIR__ . '/';
  4. private $cl;
  5. /*
  6. * 动态传入QQ或WX字符串,自动转换对应的api类登录
  7. */
  8. public function __construct($type)
  9. {
  10. //注册自动加载函数
  11. spl_autoload_register([$this,'Psr4Autoload']);
  12. //引入curl
  13. $this->cl = new $type();
  14. }
  15. public function Psr4Autoload($class)
  16. {
  17. $class_file = $this->path .'/'. $class . '.php';
  18. if (file_exists($class_file))
  19. {
  20. include "$class_file";
  21. }else{
  22. die('类文件'.$class_file .'不存在');
  23. }
  24. }
  25. public function QRcode()
  26. {
  27. return call_user_func([$this->cl,__FUNCTION__]);
  28. }
  29. public function ListenQR(...$args)
  30. {
  31. return call_user_func([$this->cl,__FUNCTION__],$args);
  32. }
  33. public function __call($name, $arguments)
  34. {
  35. call_user_func_array([$this->cl,$name],(array)$arguments);
  36. }
  37. }
以及最后一个curl类:
  1. <?php
  2. class Curl_Api
  3. {
  4. public $ua = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36";
  5. public function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0){
  6. $ch = curl_init();
  7. curl_setopt($ch, CURLOPT_URL,$url);
  8. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  9. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  10. $httpheader[] = "Accept: application/json";
  11. $httpheader[] = "Accept-Encoding: gzip,deflate,sdch";
  12. $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
  13. $httpheader[] = "Connection: keep-alive";
  14. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
  15. if($post){
  16. curl_setopt($ch, CURLOPT_POST, 1);
  17. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  18. }
  19. if($header){
  20. curl_setopt($ch, CURLOPT_HEADER, TRUE);
  21. }
  22. if($cookie){
  23. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  24. }
  25. if($referer){
  26. curl_setopt($ch, CURLOPT_REFERER, $referer);
  27. }
  28. if($ua){
  29. curl_setopt($ch, CURLOPT_USERAGENT,$ua);
  30. }else{
  31. curl_setopt($ch, CURLOPT_USERAGENT,$this->ua);
  32. }
  33. if($nobaody){
  34. curl_setopt($ch, CURLOPT_NOBODY,1);
  35. }
  36. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  37. curl_setopt($ch, CURLOPT_ENCODING, "gzip");
  38. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  39. $ret = curl_exec($ch);
  40. curl_close($ch);
  41. return $ret;
  42. }
  43. function curl($url, $paras = array()) {
  44. $ch = curl_init();
  45. curl_setopt($ch, CURLOPT_URL, $url);
  46. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  47. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  48. $httpheader[] = "Accept:*/*";
  49. $httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
  50. $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
  51. $httpheader[] = "Connection:close";
  52. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
  53. if ($paras['ctime']) { // 连接超时
  54. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $paras['ctime']);
  55. }
  56. if ($paras['rtime']) { // 读取超时
  57. curl_setopt($ch, CURLOPT_TIMEOUT_MS, $paras['rtime']);
  58. }
  59. if ($paras['post']) {
  60. curl_setopt($ch, CURLOPT_POST, 1);
  61. curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
  62. }
  63. if ($paras['header']) {
  64. curl_setopt($ch, CURLOPT_HEADER, true);
  65. }
  66. if ($paras['cookie']) {
  67. curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']);
  68. }
  69. if ($paras['refer']) {
  70. if ($paras['refer'] == 1) {
  71. curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
  72. } else {
  73. curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
  74. }
  75. }
  76. if ($paras['ua']) {
  77. curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']);
  78. } else {
  79. 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");
  80. }
  81. if ($paras['nobody']) {
  82. curl_setopt($ch, CURLOPT_NOBODY, 1);
  83. }
  84. curl_setopt($ch, CURLOPT_ENCODING, "gzip");
  85. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  86. $ret = curl_exec($ch);
  87. curl_close($ch);
  88. return $ret;
  89. }
  90. public function get_curl_split($url){
  91. $ch = curl_init();
  92. curl_setopt($ch, CURLOPT_URL,$url);
  93. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  94. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  95. $httpheader[] = "Accept: */*";
  96. $httpheader[] = "Accept-Encoding: gzip,deflate,sdch";
  97. $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
  98. $httpheader[] = "Connection: keep-alive";
  99. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
  100. curl_setopt($ch, CURLOPT_HEADER, TRUE);
  101. curl_setopt($ch, CURLOPT_USERAGENT,$this->ua);
  102. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  103. curl_setopt($ch, CURLOPT_ENCODING, "gzip");
  104. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  105. $ret = curl_exec($ch);
  106. $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  107. $header = substr($ret, 0, $headerSize);
  108. $body = substr($ret, $headerSize);
  109. $ret=array();
  110. $ret['header']=$header;
  111. $ret['body']=$body;
  112. curl_close($ch);
  113. return $ret;
  114. }
  115. }
大致调用的流程
1、保存四个类到文件里面
2、引入文件
3、单独写两个接口,一个生成qr码(base64),一个轮询二维码扫码状态
4、用户扫码成功后、会返回一个QQ号火微信唯一id
5、这里我只演示一个微信扫码登陆的例子
生成二维码并轮询检测二维码状态 login.php :
  1. <?php
  2. //我这里只引入了一个文件的原因是因为Wechat和QQ类不用引入、只需要把Curl_Api请求类引入进来就好,但我Tencent类内已经引入了。所以这里我只需要引入一个文件就好
  3. include "Lib/Tencent/Tencent.php";
  4. $wx = new Tencent("Wechat");
  5. $ret = $wx->QRcode();
  6. ?>
  7. <!--直接生成QR码、记得把uuid给带上-->
  8. <img id="wx" src="data:text/html;base64,<?=$ret['qrcode']?>" uuid="<?=$ret['uuid']?>">
  9. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  10. <script>
  11. // setTimeout(function () {
  12. //     var uuid = document.getElementById('wx').getAttribute("uuid");
  13. //     var url ="/ajax.php?uuid="+uuid;
  14. //     console.log(url);
  15. // },1000);
  16. //每秒去查询一次二维码状态
  17. $(document).ready(function () {
  18. setInterval(function () {
  19. var uuid = document.getElementById('wx').getAttribute("uuid");
  20. var url ="/ajax.php?uuid="+uuid;
  21. $.ajax({type:"GET",url:url,success:function (data) {
  22. if (data.code == 200)
  23. {
  24. alert("登陆成功,uin为:"+data.uid);
  25. }
  26. }});
  27. },1000);
  28. });
  29. </script>
AJAX.php :
  1. <?php
  2. include "Lib/Tencent/Tencent.php";
  3. $wx = new Tencent("Wechat");
  4. //直接获取到uuid后,监听就好了
  5. $ret = $wx->ListenQR($_GET['uuid']);
  6. //var_dump($ret);
  7. echo json_encode($ret,true);exit;
  8. 这个例子是微信的,QQ同样的代码一样可以运行
数据库用户表多一个qq和wxuin字段、用于保存用户绑定的QQ和微信

免责声明:文中图文均系网友发布,转载来自网络,如有侵权请联系右侧客服QQ删除,无忧屋网友发布此文仅为传递信息,不代表无忧屋平台认同其观点。