1.前提
网上很多web协议可参考,亲自实践,主要流程1.获取uuid用
2.生成二维码地址
3.扫描登录检测
4.扫码回调设置cookie
5.微信初始化
6.开启微信通知获取联系人、心跳检查、信息获取等等
注意事项:
1、不同版本的有可能产生的请求地址不一样,从扫码回调接口获得,比如最新的版的请求wx.qq.com,旧版本的请求wx2.qq.com;
2、网上看识别公众号个人通过ContactFlag识别,此方法已经失效;通过分析可以KeyWord识别;N多之前的总结随着更新都会失效,需要自己分析
3、web协议随时更新甚至消失,所以现在的结论也只是暂时的以及代码;
2.效果
时时收发消息记录:
发消息了,php之路......走起
3.代码
核心代码,简单易懂粗陋(实现了登录、收发消息,获取联系人) 其他的就不发布了
<?php
/**
* Desc: 微信web核心协议实现
* Author qq43599939
* Url http://blog.csdn.net/qq43599939
* Date: 2018/03/12
*/
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/qrcode/{$uuid}";
return $url;
}
/**
* 扫描登录检测
*/
public function login($uuid)
{
$url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&r=' . ~time() . '&uuid=' . $uuid . '&tip=0&_=' . $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' => 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));
}
}
回头给个完整版下载地址