见图:
 
 
 
 
 
<?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>
    onload=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>
 
版权声明:本文为钢锅原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhouein/p/16813047.html