当前位置:首页 > PHP教程 > php应用 > 列表

PHP实现的微信公众号扫码模拟登录功能示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-11-24 11:06:45 浏览: 评论:0 

这篇文章主要介绍了PHP实现的微信公众号扫码模拟登录功能,涉及php针对微信公众平台接口的调用与交互相关操作技巧,需要的朋友可以参考下。

本文实例讲述了PHP实现的微信公众号扫码模拟登录功能,分享给大家供大家参考,具体如下:

PHP微信公众号扫码模拟登录功能

功能只是将:https://github.com/huanz/wechat-mp-hack 改成PHP实现罢了.

之前有个休闲豆每日晨报订阅号每天定时群发消息,去年微信突然要求一定要扫码授权才能登录,FK,然后就放弃了,前几天看到早有人使用程序扫码登录,获取token,cookie自动群发了,闲着也是闲着,就将js改成php实现了登录功能.

主要流程如下

1,先访问https://mp.weixin.qq.com/ ,模拟登录,进入二维码页面

2,带着返回的cookie下载二维码.程序后台一直while循环,等待扫描消息.

3,打开下载的二维码,微信扫码,登录成功,获取token和cookie,然后后面就可以自由发挥了.

供上代码.

  1. class WeiSendAuto 
  2.   //--------------------------------------------------------LOGIN START 
  3.   private $_apis = [ 
  4.     "host"     => "https://mp.weixin.qq.com"
  5.     "login"     => "https://mp.weixin.qq.com/cgi-bin/bizlogin?action=startlogin"
  6.     "qrcode"    => "https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=getqrcode¶m=4300"
  7.     "loginqrcode"  => "https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=ask&token=&lang=zh_CN&f=json&ajax=1"
  8.     "loginask"   => "https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=ask&token=&lang=zh_CN&f=json&ajax=1&random="
  9.     "loginauth"   => "https://mp.weixin.qq.com/cgi-bin/loginauth?action=ask&token=&lang=zh_CN&f=json&ajax=1"
  10.     "bizlogin"   => "https://mp.weixin.qq.com/cgi-bin/bizlogin?action=login&lang=zh_CN" 
  11.   ]; 
  12.   private $_redirect_url = ""
  13.   private $_key      = ""
  14.   private function _getCookieFile(){ 
  15.     return WEI_UPLOAD_PATH."cookie_{$this->_key}.text"
  16.   } 
  17.   private function _getSavePath(){ 
  18.     return WEI_UPLOAD_PATH.$this->_qrcodeName(); 
  19.   } 
  20.   private function _qrcodeName(){ 
  21.     return "qrcode_{$this->_key}.png"
  22.   } 
  23.   private function _log($msg){ 
  24.     Log::record("[微信调度:".date("Y-m-d H:i:s")."] ======: {$msg}"); 
  25.   } 
  26.   public function getToken(){ 
  27.     return Utils::getCache("token_{$this->_key}"); 
  28.   } 
  29.   public function setToken($token){ 
  30.      Utils::setCache("token_{$this->_key}",$token); 
  31.   } 
  32.   public function init($options){ 
  33.     if(!isset($options["key"])){ 
  34.       die("Key is Null!"); 
  35.     } 
  36.     $this->_key   =  $options["key"]; 
  37.     if($this->getToken()){ 
  38.       echo("HAS Token !"); 
  39.       return
  40.     }else
  41.       //尼玛,先要获取首页!!! 
  42.       $this->fetch("https://mp.weixin.qq.com/","","text"); 
  43.       $this->_log("start login!!"); 
  44.       $this->start_login($options); 
  45.     } 
  46.   } 
  47.   private function start_login($options){ 
  48.     $_res    = $this->_login($options["account"],$options["password"]); 
  49.     if(!$_res["status"]){ 
  50.       $this->_log($_res["info"]); 
  51.       return
  52.     } 
  53.     //保存二维码 
  54.     $this->_saveQRcode(); 
  55.     $_ask_api    =  $this->_apis["loginask"]; 
  56.     $_input["refer"] =  $this->_redirect_url; 
  57.     $_index     =  1; 
  58.     while(true){ 
  59. /*      if($_index>60){ 
  60.         break; 
  61.       }*/ 
  62.       $_res    =  $this->fetch($_ask_api.$this->getWxRandomNum(),$_input); 
  63.       $_status   =  $_res["status"]; 
  64.       if($_status==1){ 
  65.         if($_res["user_category"]==1){ 
  66.           $_ask_api = $this->_apis["loginauth"]; 
  67.         }else
  68.           $this->_log("Login success"); 
  69.           break
  70.         } 
  71.       }else if($_status==4){ 
  72.         $this->_log("已经扫码"); 
  73.       }else if($_status==2){ 
  74.         $this->_log("管理员拒绝"); 
  75.         break
  76.       }else if($_status==3){ 
  77.         $this->_log("登录超时"); 
  78.         break
  79.       }else
  80.         if($_ask_api==$this->_apis["loginask"]){ 
  81.           $this->_log("请打开test.jpg,用微信扫码"); 
  82.         }else
  83.           $this->_log("等待确认"); 
  84.         } 
  85.       } 
  86.       sleep(2); 
  87.       $_index++; 
  88.     } 
  89.     /*if($_index>=60){ 
  90.       $this->_log("U亲,超时了"); 
  91.       return; 
  92.     }*/ 
  93.     $this->_log("开始验证"); 
  94.     $_input["post"]   = ["lang"=>"zh_CN","f"=>"json","ajax"=>1,"random"=>$this->getWxRandomNum(),"token"=>""]; 
  95.     $_input["refer"]   = $this->_redirect_url; 
  96.     $_res        = $this->fetch($this->_apis["bizlogin"],$_input); 
  97.     $this->_log(print_r($_res,true)); 
  98.     if($_res["base_resp"]["ret"]!=0){ 
  99.       $this->_log("error = ".$_res["base_resp"]["err_msg"]); 
  100.       return ; 
  101.     } 
  102.     $redirect_url    =  $_res["redirect_url"];//跳转路径 
  103.     if(preg_match('/token=([\d]+)/i'$redirect_url,$match)){//获取cookie 
  104.       $this->setToken($match[1]); 
  105.     } 
  106.     $this->_log("验证成功,token: ".$this->getToken()); 
  107.   } 
  108.   //下载二维码 
  109.   private function _saveQRcode(){ 
  110.     $_input["refer"] = $this->_redirect_url; 
  111.     $_res    = $this->fetch($this->_apis["qrcode"],$_input,"text"); 
  112.     $fp     = fopen($this->_getSavePath(), "wb+"or die("open fails"); 
  113.     fwrite($fp,$_resor die("fwrite fails"); 
  114.     fclose($fp); 
  115.   } 
  116.   private function _login($_username,$_password){ 
  117.     $_input["post"] = array
  118.       'username'  => $_username
  119.       'pwd'    => md5($_password), 
  120.       'f'     => 'json'
  121.       'imgcode'  => "" 
  122.     ); 
  123.     $_input["refer"] = "https://mp.weixin.qq.com"
  124.     $_res      = $this->fetch($this->_apis["login"],$_input); 
  125.     if($_res["base_resp"]["ret"]!==0){ 
  126.       return Utils::error($_res["base_resp"]["err_msg"]); 
  127.     } 
  128.     $this->_redirect_url  =  "https://mp.weixin.qq.com".$_res["redirect_url"];//跳转路径 
  129.     return Utils::success("ok"); 
  130.   } 
  131.   function getWxRandomNum(){ 
  132.     return "0.".mt_rand(1000000000000000,9999999999999999); 
  133.   } 
  134.   /** 
  135.    * @param $url 
  136.    * @param null $_input 
  137.    * @param string $data_type 
  138.    * @return mixed 
  139.    * $_input= ["post"=>[],"refer"=>"",cookiefile=''] 
  140.    */ 
  141.   function fetch( $url$_input=null, $data_type='json') { 
  142.     $ch = curl_init(); 
  143.     $useragent = isset($_input['useragent']) ? $_input['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2'
  144.     //curl_setopt( $ch, CURLOPT_HTTPHEADER, $this->_headers); //设置HTTP头字段的数组 
  145.     curl_setopt( $ch, CURLOPT_URL, $url ); 
  146.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); 
  147.     curl_setopt( $ch, CURLOPT_AUTOREFERER, true ); 
  148.     curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); 
  149.     curl_setopt( $ch, CURLOPT_POST, isset($_input['post']) ); 
  150.     if( isset($_input['post']) )     curl_setopt( $ch, CURLOPT_POSTFIELDS, $_input['post'] ); 
  151.     if( isset($_input['refer']) )    curl_setopt( $ch, CURLOPT_REFERER, $_input['refer'] ); 
  152.     curl_setopt( $ch, CURLOPT_USERAGENT, $useragent ); 
  153.     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($_input['timeout']) ? $_input['timeout'] : 5 ) ); 
  154.     curl_setopt( $ch, CURLOPT_COOKIEJAR, ( isset($_input['cookiefile']) ? $_input['cookiefile'] : $this->_getCookieFile() )); 
  155.     curl_setopt( $ch, CURLOPT_COOKIEFILE, ( isset($_input['cookiefile']) ? $_input['cookiefile'] : $this->_getCookieFile() )); 
  156.     $result = curl_exec( $ch ); 
  157.     curl_close( $ch ); 
  158.     if ($data_type == 'json') { 
  159.       $result = json_decode($result,true); 
  160.     } 
  161.     return $result
  162.   } 
  163.   //--------------------------------------------------------LOGIN END 

怎么调用?上码

  1. $arr = array
  2.   'account'  => '***'
  3.   'password' => '****'
  4.   'key'    => "tmall"
  5. ); 
  6. $w       =  new WeiSendAuto(); 
  7. $w->init($arr); 
  8. if(!$w->getToken()){ 
  9.   die("NOT TOKEN!"); 
  10. }

Tags: PHP微信公众号扫码模拟登录

分享到: