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

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

怎么调用?上码

  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. }