首页 > 编程语言 >PHP版web 微信协议模拟登录

PHP版web 微信协议模拟登录

时间:2022-10-21 12:23:10浏览次数:45  
标签:web ch url 微信 COOKIE curl PHP data CURLOPT

见图:          

<?php
/**
* Desc: 微信web核心协议实现
*/
function array_to_json($data) {
    $data = json_encode($data,JSON_UNESCAPED_UNICODE);
    return $data;
}
include 'webweixin\phpqrcode.php';
class WxCore{
    private $appid = 'wx782c26e4c19acffb';
    /**
    * 获取uuid用
    */
    public function get_uuid(){
        $url = 'https://login.weixin.qq.com/jslogin';
        $url .= '?appid=' . $this->appid;
        $url .= '&fun=new';
        $url .= '&lang=zh_CN';
        $url .= '&_=' . $this->getMillisecond();
        $content = $this->curlPost($url);
        $content = explode(';', $content);
        $content_uuid = explode('"', $content[1]);
        $uuid = $content_uuid[1];
        return $uuid;
    }
    /**
    * 生成二维码地址
    */
    public function qrcode($uuid){
        $url = "https://login.weixin.qq.com/l/{$uuid}";
        return $url;
    }
    /**
    * 扫描登录检测
    */
    public function login($uuid){
        $url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid='. $uuid .'&tip=1&r=1249597736&_='. $this->getMillisecond();
        $content = $this->curlPost($url);
        preg_match('/\d+/', $content, $match);
        $code = $match[0];
        if($code==200){
            preg_match('/((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/', $content, $returnmsg);
            $returnurl = $returnmsg[0];
            $returnurl = rtrim($returnurl,'";');
            if ($returnurl) {
                $data = array(
                    'code' => $code,
                    'returnurl' => $returnurl,
                    //'returnurl' => urlencode($returnurl),
                );
            }
        }else if($code==201){
            preg_match('/([\'"])([^\'"\.]*?)\1/', $content, $returnmsg);
            $user_icon = $returnmsg[2];
            if ($user_icon) {
                $data = array(
                'code' => $code,
                'icon' => $user_icon,
                );
            }
        }else{
            $data['code'] = $code;
        }
            return ($data);
    }
    /**
    * 扫描登录检测
    */
    public function login_para($url){
        $url = $url . '&fun=new&version=v2';
        $content = $this->curlPost($url,'',true);
        preg_match_all('/^Set-Cookie: (.*?);/m',$content,$cookies);
        $cookies_str = '';
        if($cookies[1]){
            foreach($cookies[1] as $v){
                $cookies_str .= $v. ';';
            }
        }
        setcookie("cookies_str", $cookies_str, time()+3600); //回调地址获取cookies
        $data['url'] = $url;
        preg_match('/<error>[\s\S]+/', $content, $returnmsg);
        $xml = simplexml_load_string($returnmsg[0]);
        $val = json_decode(json_encode($xml),true);
        $data['content'] = $val;
        return ($data);
    }
    /**
    * 微信初始化
    */
    public function wxinit(){
        $BaseRequest = array(
            'DeviceID' => $_COOKIE['DeviceID'],
            'Sid' => $_COOKIE['Sid'],
            'Skey' => $_COOKIE['Skey'],
            'Uin' => $_COOKIE['Uin']
        );
        $url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?pass_ticket=' . $_COOKIE['pass_ticket'] . '&skey='.$_COOKIE['Skey'].'&lang=zh_CN&r=' . time();
        $params = array(
            'BaseRequest' => $BaseRequest,
        );
        $json = $this->curlCookiePost($url, $params,$_COOKIE['cookies_str']);
        return $json;
    }
    /**
    * 开启微信通知
    */
    public function wxstatusnotify($userid){
        $BaseRequest = array(
            'DeviceID' => $_COOKIE['DeviceID'],
            'Sid' => $_COOKIE['Sid'],
            'Skey' => $_COOKIE['Skey'],
            'Uin' => $_COOKIE['Uin']
        );
        $url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify?lang=zh_CN&pass_ticket=' . $_COOKIE['pass_ticket'];
        $params = array(
            'BaseRequest' => $BaseRequest,
            "Code" => 3,
            "FromUserName" => $userid,
            "ToUserName" => $userid,
            "ClientMsgId" => $this->getMillisecond()
        );
        $data = $this->curlCookiePost($url, $params,$_COOKIE['cookies_str']);
        return $data;
    }
    /**
    * 获取联系人
    */
    public function webwxgetcontact(){
        $url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?lang=zh_CN&pass_ticket='.$_COOKIE['pass_ticket'].'&r='.$this->getMillisecond().'&seq=0&skey='.$_COOKIE['Skey'];
        $data = $this->curlCookiePost($url,'',$_COOKIE['cookies_str']);
        return $data;
    }
    /**
    * 心跳检测
    */
    public function synccheck(){
        $url = "https://webpush.wx.qq.com/cgi-bin/mmwebwx-bin/synccheck?r=" . $this->getMillisecond() . mt_rand(11111,99999) . "&skey="
        . urlencode($_COOKIE['Skey']) . "&sid=" . urlencode($_COOKIE['Sid']) . "&deviceid=" . urlencode($_COOKIE['DeviceID']) .
        "&uin=" . urlencode($_COOKIE['Uin']) . "&synckey=" . urlencode($_COOKIE['synckey']) . "&_=" . $this->getMillisecond();
        $data = $this->curlCookiePost($url,'',$_COOKIE['cookies_str']);
        $rule = '/window.synccheck={retcode:"(\d+)",selector:"(\d+)"}/';
        preg_match($rule, $data, $match);
        $status = array(
            'ret' => $match[1],
            'sel' => $match[2],
        );
        return $status;
    }
    /**
    * 获取最新消息
    */
    public function webwxsync(){
        $BaseRequest = array(
            'DeviceID' => $_COOKIE['DeviceID'],
            'Sid' => $_COOKIE['Sid'],
            'Skey' => $_COOKIE['Skey'],
            'Uin' => $_COOKIE['Uin']
        );
        $url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=' . $_COOKIE['Sid'] . '&skey=' . $_COOKIE['Skey'] . '&pass_ticket=' . $_COOKIE['pass_ticket'];
        $SyncKey_arr = array();
        $temp_arr = explode('|', $_COOKIE['synckey']);
        $SyncKey_arr['Count'] = count($temp_arr);
        foreach($temp_arr as $v){
            $temp_arr_v = explode('_', $v);
            $SyncKey_arr_temp['Key'] = $temp_arr_v[0];
            $SyncKey_arr_temp['Val'] = $temp_arr_v[1];
            $SyncKey_arr['List'][] = $SyncKey_arr_temp;
        }
        $params = array(
            'BaseRequest' => $BaseRequest,
            'SyncKey' => $SyncKey_arr,
            'rr' => $this->getMillisecond(),
        );
        $data = $this->curlCookiePost($url, $params,$_COOKIE['cookies_str']);
        return $data;
    }
    /**
    * 发送消息
    */
    public function webwxsendmsg($toid,$content){
        $BaseRequest = array(
            'DeviceID' => $_COOKIE['DeviceID'],
            'Sid' => $_COOKIE['Sid'],
            'Skey' => $_COOKIE['Skey'],
            'Uin' => $_COOKIE['Uin']
        );
        $url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?lang=zh_CN&pass_ticket=' . $_COOKIE['pass_ticket'];
        $clientMsgId = time() * 1000 + rand(1000, 9999);
        $params = array(
            'BaseRequest' => $BaseRequest,
            'Msg' => array(
                "Type" => 1,
                "Content" => $content,
                "FromUserName" => $_COOKIE['myUserName'],
                "ToUserName" => $toid,
                "LocalID" => $clientMsgId,
                "ClientMsgId" => $clientMsgId
            ),
            'Scene' => 0,
        );
        $data = $this->curlCookiePost($url, $params,$_COOKIE['cookies_str']);
        return $data;
    }
    /**
    *退出登录
    */
    public function wxloginout(){
        $url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout?redirect=1&type=1&skey=' . urlencode($_COOKIE['Skey']);
        $param = array(
            'sid' => $_COOKIE['Sid'],
            'uin' => $_COOKIE['Uin'],
        );
        $this->curlPost($url, $param);
        return true;
    }
    public function curlPost($url, $data=array(), $is_getcookie='', $timeout = 30){
        $header = array(
            'Accept' => 'application/json, text/plain, */*',
            'Accept-Language' => 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,ja;q=0.2',
            'Host' => 'wx.qq.com',
            'Referer' => 'https://wx.qq.com/?lang=zh_CN',
            'Content-Type' => 'application/json;charset=UTF-8',
            'Connection' => 'Keep-Alive'
        );
        $user_agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT,$user_agent);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        if ($data) {
            $data = json_encode($data);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        if ($is_getcookie) {
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_HEADER,1);
        }
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }
    public function curlCookiePost($url, $data=array(), $cookiedata='', $timeout = 30){
        $header = array(
            'Accept' => 'application/json, text/plain, */*',
            'Accept-Language' => 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,ja;q=0.2',
            'Host' => 'wx.qq.com',
            'Referer' => 'https://wx.qq.com/?lang=zh_CN',
            'Content-Type' => 'application/json;charset=UTF-8',
            'Connection' => 'Keep-Alive'
        );
        $user_agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT,$user_agent);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        if ($data) {
            $data = json_encode($data,JSON_UNESCAPED_UNICODE); //防止中文转义
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        if ($cookiedata) {
            curl_setopt($ch,CURLOPT_COOKIE,$cookiedata);
        }
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }
    public function getMillisecond(){
        list($t1, $t2) = explode(' ', microtime());
        return $t2 . ceil(($t1 * 1000));
    }
}
    $run = new WxCore();
    $uuid = $run -> get_uuid();
    $erweimaUrl = $run -> qrcode($uuid);
    $errorCorrectionLevel = "L";
    $matrixPointSize = "10";
    QRcode::png($erweimaUrl, 'helloweixin.png');
if(isset($_REQUEST['uuid'])){
    $uuid = $_REQUEST['uuid'];
    $login_check = $run -> login($uuid);
    echo array_to_json($login_check);  //{"code":"408"}
    exit;
}
?>
<body style="background: rgb(68 68 68 / 53%);">
<p style="text-align: center; font-size: 40px;color: #FFF; padding: 60px;">web版weixin模拟登录</p>
<img id="img" style = "width: 301px; height: 297px; display:block; margin:0 auto;padding: 20px; border-radius: 40px;" src="helloweixin.png">
<script src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<p id="time" style="text-align: center; background: #4CAF50; margin: auto; height: 30px; border-radius: 6px; width: 400px; padding: 10px 40px; font-size: 22px;color: #FFF;">请使用手机微信扫码!</p>
<script>
    onl oad=function(){
        setInterval(lunxun, 5000);
    };
    var x=25; //利用了全局变量来执行
    function lunxun(){
            x--;
            if(x>0){
                $.ajax({
                    data:"uuid=<?php echo $uuid;?>",
                    type: "GET",
                    async: true, //异步执行 默认是true异步
                    url: "",
                    dataType: "json",
                    success: function(data, status, xhr){
                        if(data.code==408){
                            $("#time").html('请扫码登录,状态码为:'+data.code);
                        }
                        if(data.code==201){
                            $("#time").html('已经扫码,状态码为:'+data.code);
                            $("img").attr("src",data.icon);
                        }
                        if(data.code==200){
                            $("#time").html('已经扫码,登录成功,状态码为:'+data.code);
                            //alert(data.returnurl);
                            //window.location.href=data.returnurl;
                        }
                    }
                });
           }else{
                location.href='#';  // 循环5次后跳转到注册页面
            }
    }
</script>
</body>
 

标签:web,ch,url,微信,COOKIE,curl,PHP,data,CURLOPT
From: https://www.cnblogs.com/zhouein/p/16813047.html

相关文章

  • PHP保留两位小数的几种方法
    这篇文章主要介绍了PHP保留两位小数的几种方法,需要的朋友可以参考下 代码如下所示:$num=10.4567;//第一种:利用round()对浮点数进行四舍五入echoround($num,2......
  • Web开发
    一、静态资源探究stringboot对于静态资源放置的位置有规定的在WebMvcAutoConfiguration配置类中,我们可以用一下方式处理静态资源@OverridepublicvoidaddReso......
  • js php 简单聊天室
    本文使用websocket+gatewayworker搭的一个简单聊天室,随便写了点样式,大家不要介意哈,主要看流程就好 html代码,<!doctypehtml><htmllang="en"><head><meta......
  • Linux下,PHP添加PDF扩展
    CentOS7中安装在CentOS中可以直接使用Yum进行安装,除了安装ImageMagick,还要安装它的两个依赖ImageMagick-devel和ImageMagick-perl。yum install -y ImageMagick Ima......
  • Webpack完整打包流程分析
    前言webpack在前端工程领域起到了中流砥柱的作用,理解它的内部实现机制会对你的工程建设提供很大的帮助(不论是定制功能还是优化打包)。下面我们基于webpack5源码结构,对......
  • Web3 每日资讯(2022-10-20)
    观风向,赢未来。03:24华纳兄弟将发行《指环王》系列NFT04:20特斯拉Q3财报:比特币持仓量保持不变06:51Solana联合创始人表示,Web3手机是对抗谷歌和苹果的机会,目前Web3智......
  • Webpack中的plugin插件机制
    大家有没有遇到过这些问题:webpack打包之后的文件没有压缩静态文件要手动拷贝到输出目录代码中写了很多环境判断的多余代码上一篇「webpack核心特性」loader说到......
  • WebSocket是什么
    WebSocketWebSocket(ws)也可以称之为一种协议,提供给我们基于TCP之上的,能够让服务器和客户端互相发送信息的这么一种协议。whynothttp最开始我其实没有搞清楚http和ws的......
  • 第一个JavaWeb项目
    第一个JavaWeb项目1、Maven的搭建1.1、Maven环境的配置与安装下载:[maven的下载地址](https://maven.apache.org/)环境配置:系统变量配置path变量配置1.2、构建......
  • JavaWeb完整案例详细步骤
    JavaWeb完整案例详细步骤废话少说,展示完整案例主要实现功能基本的CURD、分页查询、条件查询、批量删除所使用的技术前端:Vue+Ajax+Elememt-ui后端:Web层(Servlet)+Serv......