侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

微信(小程序)  /  管理员 发布于 8年前   208

本文实例讲述了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,然后后面就可以自由发挥了.

供上代码.

class WeiSendAuto{  //--------------------------------------------------------LOGIN START  private $_apis = [    "host"     => "https://mp.weixin.qq.com",    "login"     => "https://mp.weixin.qq.com/cgi-bin/bizlogin?action=startlogin",    "qrcode"    => "https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=getqrcode¶m=4300",    "loginqrcode"  => "https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=ask&token=&lang=zh_CN&f=json&ajax=1",    "loginask"   => "https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=ask&token=&lang=zh_CN&f=json&ajax=1&random=",    "loginauth"   => "https://mp.weixin.qq.com/cgi-bin/loginauth?action=ask&token=&lang=zh_CN&f=json&ajax=1",    "bizlogin"   => "https://mp.weixin.qq.com/cgi-bin/bizlogin?action=login&lang=zh_CN"  ];  private $_redirect_url = "";  private $_key      = "";  private function _getCookieFile(){    return WEI_UPLOAD_PATH."cookie_{$this->_key}.text";  }  private function _getSavePath(){    return WEI_UPLOAD_PATH.$this->_qrcodeName();  }  private function _qrcodeName(){    return "qrcode_{$this->_key}.png";  }  private function _log($msg){    Log::record("[微信调度:".date("Y-m-d H:i:s")."] ======: {$msg}");  }  public function getToken(){    return Utils::getCache("token_{$this->_key}");  }  public function setToken($token){     Utils::setCache("token_{$this->_key}",$token);  }  public function init($options){    if(!isset($options["key"])){      die("Key is Null!");    }    $this->_key   =  $options["key"];    if($this->getToken()){      echo("HAS Token !");      return;    }else{      //尼玛,先要获取首页!!!      $this->fetch("https://mp.weixin.qq.com/","","text");      $this->_log("start login!!");      $this->start_login($options);    }  }  private function start_login($options){    $_res    = $this->_login($options["account"],$options["password"]);    if(!$_res["status"]){      $this->_log($_res["info"]);      return;    }    //保存二维码    $this->_saveQRcode();    $_ask_api    =  $this->_apis["loginask"];    $_input["refer"] =  $this->_redirect_url;    $_index     =  1;    while(true){/*      if($_index>60){        break;      }*/      $_res    =  $this->fetch($_ask_api.$this->getWxRandomNum(),$_input);      $_status   =  $_res["status"];      if($_status==1){        if($_res["user_category"]==1){          $_ask_api = $this->_apis["loginauth"];        }else{          $this->_log("Login success");          break;        }      }else if($_status==4){        $this->_log("已经扫码");      }else if($_status==2){        $this->_log("管理员拒绝");        break;      }else if($_status==3){        $this->_log("登录超时");        break;      }else{        if($_ask_api==$this->_apis["loginask"]){          $this->_log("请打开test.jpg,用微信扫码");        }else{          $this->_log("等待确认");        }      }      sleep(2);      $_index++;    }    /*if($_index>=60){      $this->_log("U亲,超时了");      return;    }*/    $this->_log("开始验证");    $_input["post"]   = ["lang"=>"zh_CN","f"=>"json","ajax"=>1,"random"=>$this->getWxRandomNum(),"token"=>""];    $_input["refer"]   = $this->_redirect_url;    $_res        = $this->fetch($this->_apis["bizlogin"],$_input);    $this->_log(print_r($_res,true));    if($_res["base_resp"]["ret"]!=0){      $this->_log("error = ".$_res["base_resp"]["err_msg"]);      return ;    }    $redirect_url    =  $_res["redirect_url"];//跳转路径    if(preg_match('/token=([\d]+)/i', $redirect_url,$match)){//获取cookie      $this->setToken($match[1]);    }    $this->_log("验证成功,token: ".$this->getToken());  }  //下载二维码  private function _saveQRcode(){    $_input["refer"] = $this->_redirect_url;    $_res    = $this->fetch($this->_apis["qrcode"],$_input,"text");    $fp     = fopen($this->_getSavePath(), "wb+") or die("open fails");    fwrite($fp,$_res) or die("fwrite fails");    fclose($fp);  }  private function _login($_username,$_password){    $_input["post"] = array(      'username'  => $_username,      'pwd'    => md5($_password),      'f'     => 'json',      'imgcode'  => ""    );    $_input["refer"] = "https://mp.weixin.qq.com";    $_res      = $this->fetch($this->_apis["login"],$_input);    if($_res["base_resp"]["ret"]!==0){      return Utils::error($_res["base_resp"]["err_msg"]);    }    $this->_redirect_url  =  "https://mp.weixin.qq.com".$_res["redirect_url"];//跳转路径    return Utils::success("ok");  }  function getWxRandomNum(){    return "0.".mt_rand(1000000000000000,9999999999999999);  }  /**   * @param $url   * @param null $_input   * @param string $data_type   * @return mixed   * $_input= ["post"=>[],"refer"=>"",cookiefile='']   */  function fetch( $url, $_input=null, $data_type='json') {    $ch = curl_init();    $useragent = isset($_input['useragent']) ? $_input['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';    //curl_setopt( $ch, CURLOPT_HTTPHEADER, $this->_headers); //设置HTTP头字段的数组    curl_setopt( $ch, CURLOPT_URL, $url );    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );    curl_setopt( $ch, CURLOPT_POST, isset($_input['post']) );    if( isset($_input['post']) )     curl_setopt( $ch, CURLOPT_POSTFIELDS, $_input['post'] );    if( isset($_input['refer']) )    curl_setopt( $ch, CURLOPT_REFERER, $_input['refer'] );    curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($_input['timeout']) ? $_input['timeout'] : 5 ) );    curl_setopt( $ch, CURLOPT_COOKIEJAR, ( isset($_input['cookiefile']) ? $_input['cookiefile'] : $this->_getCookieFile() ));    curl_setopt( $ch, CURLOPT_COOKIEFILE, ( isset($_input['cookiefile']) ? $_input['cookiefile'] : $this->_getCookieFile() ));    $result = curl_exec( $ch );    curl_close( $ch );    if ($data_type == 'json') {      $result = json_decode($result,true);    }    return $result;  }  //--------------------------------------------------------LOGIN END}

怎么调用?上码

$arr = array(  'account'  => '***',  'password' => '****',  'key'    => "tmall",);$w       =  new WeiSendAuto();$w->init($arr);if(!$w->getToken()){  die("NOT TOKEN!");}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP微信开发技巧汇总》、《php curl用法总结》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《PHP中json格式数据操作技巧汇总》及《PHP针对XML文件操作技巧总结》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • 微信小程序 PHP生成带参数二维码
  • php微信高级接口调用方法(自定义菜单接口、客服接口、二维码)
  • php微信开发之批量生成带参数的二维码
  • PHP微信PC二维码登陆的实现思路
  • PHP版微信第三方实现一键登录及获取用户信息的方法
  • PHP后台实现微信小程序登录
  • PHP Curl模拟登录微信公众平台、新浪微博实例代码
  • php版微信自动登录并获取昵称的方法
  • php的laravel框架快速集成微信登录的方法
  • PHP实现微信小程序人脸识别刷脸登录功能
  • PHP实现网站应用微信登录功能详解


  • 上一条:
    php能开发小程序吗
    下一条:
    PHP微信网页授权的配置文件操作分析
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 微信模板消息改版后发送规则记录(微信订阅消息参数值内容限制说明)(1个评论)
    • 微信支付v3对接所需工具及命令(0个评论)
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(1个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(2个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客