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

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

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

主要流程如下

1,先访问 ,模拟登录,进入二维码页面
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程序设计有所帮助。