-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.php
157 lines (137 loc) · 4.71 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
header('content-type:text/html;charset=utf-8;');
include 'http.php';
// 微信公众账号
$user = "xxxxxxx";
// 微信公众号登陆密码
$pass = md5(substr("xxxxxx", 0, 16));
$obj = new test($user, $pass);
$obj->sendTextMsg("接收方ID号",'你好!');
class test {
private $http;//请求实例
private $cookieLog = 'cookie.log';//cookie的保存文件
private $tokenLog = 'token.log';//token的保存文件
private $user;//用户名
private $pass;//密码
/**
* 构造函数
* @param string $user 用户名
* @param string $pass 密码
*/
public function __construct($user, $pass) {
$this->http = http::getInstance();
$this->user = $user;
$this->pass = $pass;
}
/**
* 模拟登陆
*/
private function login() {
$url = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
$post = array();
$post["username"] = $this->user;
$post["pwd"] = $this->pass;
$post["imgcode"] = '';
$post["f"] = "json";
$this->http->sendRequest($url, $post);
$data = json_decode($this->http->getResultData(), TRUE);
if (!(isset($data['ErrCode']) && $data['ErrCode'] == 0)) {
echo "登陆失败";
exit;
}
$arr = parse_url($data['ErrMsg']);
$qarr = explode('&', $arr['query']);
$token = '';
foreach ($qarr as $k=>$v) {
$karr = explode("=", $v);
if ($karr[0] == 'token') {
$token = $karr[1];
}
}
$this->write($this->tokenLog, $token);
$cookie = '';
if (preg_match_all("/set\-cookie: (.*) path/i", $this->http->getResultHeader(), $matches)) {
if (isset ($matches[1])) {
foreach ($matches[1] as $k=>$v) {
$cookie .= $v;
}
}
}
$this->write($this->cookieLog, $cookie);
}
/**
* 指定好友发送消息
* @param int $fakeId fakeid
* @param string $content 消息内容
*/
public function sendTextMsg($fakeId, $content) {
$this->checkLogin();
$cookie = $this->read($this->cookieLog);
$token = $this->read($this->tokenLog);
$header = array();
$header['Referer'] = "https://mp.weixin.qq.com/cgi-bin/singlemsgpage?token={$token}&fromfakeid={$fakeId}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";
$header['Cookie'] = $cookie;
$this->http->setHeader($header);
$post = array();
$post['token'] = $token;
$post['tofakeid'] = $fakeId;
$post['type'] = 1;
$post['content'] = $content;
$post['ajax'] = 1;
$post['error'] = 'false';
$url = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN";
$this->http->sendRequest($url, $post);
$data = json_decode($this->http->getResultData(), TRUE);
if (isset($data['ret']) && $data['ret'] == 0) {
echo '发送成功';
} else {
echo '发送失败';
}
}
/**
* 写文件
* @param string $filename 文件名
* @param string $content 内容
*/
private function write($filename, $content) {
$fp = fopen($filename, 'w');
fwrite($fp, $content);
fclose($fp);
}
/**
* 读取文件内容
* @param string $filename
* @return string
*/
private function read($filename) {
$data = '';
if (file_exists($filename)) {
$fp = fopen($filename, 'r');
$data = fread($fp, filesize($filename));
fclose($fp);
}
return $data;
}
/**
* 校验登陆
* @return boolean true
*/
private function checkLogin() {
$cookie = $this->read($this->cookieLog);
$token = $this->read($this->tokenLog);
$post = array();
$post['token'] = $token;
$post['ajax'] = 1;
$url = 'https://mp.weixin.qq.com/cgi-bin/getnewmsgnum?t=ajax-getmsgnum&lastmsgid=100002402';
$header['Referer'] = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&token={$token}&lang=zh_CN&count=50"; //伪装来源页地址 http_referer
$header['Cookie'] = $cookie;
$this->http->setHeader($header);
$this->http->sendRequest($url, $post);
$data = json_decode($this->http->getResultData(), TRUE);
if (!(isset($data['ret']) && $data['ret'] == 0)) {
$this->login();
}
return TRUE;
}
}
?>