forked from zscorpio/weChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWechat.php
253 lines (237 loc) · 6.46 KB
/
Wechat.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
define("TOKEN", "微信密钥");
define("ACCOUNT", "你的微信公众帐号");
define("PASSWORD", "你的微信公众密码");
define("METHOD", "redis或者file");
class weChatApi
{
// 构造函数
public function __construct(){
// 读取cookie
if(METHOD == 'redis'){
$this->cookie = $this->redisCookie();
}else{
$this->cookie = $this->read('cookie.log');
}
}
/**
* 检查是否是合理的请求(官方函数)
* @return boolean
*/
public function checkSignature()
{
if($_GET){
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}else{
return false;
}
}
/**
* 主动发消息
* @param string $id 用户的fakeid
* @param string $content 发送的内容
* @return [type] [description]
*/
public function send($id,$content)
{
$send_snoopy = new Snoopy;
$post = array();
$post['tofakeid'] = $id;
$post['type'] = 1;
$post['content'] = $content;
$post['ajax'] = 1;
$send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/singlemsgpage?fromfakeid={$id}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
$send_snoopy->submit($submit,$post);
return $send_snoopy->results;
}
/**
* 批量发送(可能需要设置超时)
* @param [type] $ids 用户的fakeid集合,逗号分割
* @param [type] $content [description]
* @return [type] [description]
*/
public function batSend($ids,$content)
{
$ids_array = explode(",", $ids);
$result = array();
foreach ($ids_array as $key => $value) {
$send_snoopy = new Snoopy;
$post = array();
$post['type'] = 1;
$post['content'] = $content;
$post['ajax'] = 1;
$send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/singlemsgpage?fromfakeid={$value}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
$post['tofakeid'] = $value;
$send_snoopy->submit($submit,$post);
$tmp = $send_snoopy->results;
array_push($result, $tmp);
}
return $result;
}
/**
* 获取用户的信息
* @param string $id 用户的fakeid
* @return [type] [description]
*/
public function getInfo($id)
{
$send_snoopy = new Snoopy;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "http://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid=".$id;
$send_snoopy->submit($submit,array());
$result = json_decode($send_snoopy->results,1);
if(!$result){
$this->login();
}
return $result;
}
/**
* 被动发送内容
* @param [type] $fromUsername [description]
* @param [type] $toUsername [description]
* @param [type] $msgType [description]
* @param [type] $content [description]
* @return [type] [description]
*/
public function sendText($fromUsername,$toUsername,$msgType,$content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $content);
echo $resultStr;
}
/**
* 解析数据
* @return [type] [description]
*/
public function parseData(){
$return = array();
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$postObj = json_encode($postObj);
$postObj = json_decode($postObj,1);
return $postObj;
}else {
return $return;
}
}
/**
* 模拟登录获取cookie
* @return [type] [description]
*/
public function login($locate="file"){
$snoopy = new Snoopy;
$submit = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
$post["username"] = ACCOUNT;
$post["pwd"] = md5(PASSWORD);
$post["f"] = "json";
$snoopy->submit($submit,$post);
$cookie = '';
foreach ($snoopy->headers as $key => $value) {
$value = trim($value);
if(strpos($value,'Set-Cookie: ') || strpos($value,'Set-Cookie: ')===0){
$tmp = str_replace("Set-Cookie: ","",$value);
$tmp = str_replace("Path=/","",$tmp);
$cookie.=$tmp;
}
}
if($locate == 'file'){
$this->write("cookie.log",$cookie);
}
return $cookie;
}
public function redisCookie(){
$redis = new Redis();
$redis->pconnect('127.0.0.1', 6379);
if ($redis->exists('cookie')) {
return $redis->get('cookie');
}else{
$cookie = $this->login();
$redis->setex('cookie', 600, $cookie);
return $cookie;
}
}
/**
* 把内容写入文件
* @param string $filename 文件名
* @param string $content 文件内容
* @return [type] [description]
*/
public function write($filename,$content){
$fp= fopen("./data/".$filename,"w");
fwrite($fp,$content);
fclose($fp);
}
/**
* 读取文件内容
* @param string $filename 文件名
* @return [type] [description]
*/
public function read($filename){
if(file_exists("./data/".$filename)){
$data = '';
$handle=fopen("./data/".$filename,'r');
while (!feof($handle)){
$data.=fgets($handle);
}
fclose($handle);
if($data){
$send_snoopy = new Snoopy;
$send_snoopy->rawheaders['Cookie']= $data;
$submit = "http://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid=";
$send_snoopy->submit($submit,array());
$result = json_decode($send_snoopy->results,1);
if(!$result){
return $this->login();
}else{
return $data;
}
}else{
return $this->login();
}
}else{
return $this->login();
}
}
/**
* 验证cookie的有效性
* @return [type] [description]
*/
public function checkValid()
{
$send_snoopy = new Snoopy;
$post = array();
$submit = "http://mp.weixin.qq.com/cgi-bin/getregions?id=1017&t=ajax-getregions&lang=zh_CN";
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$send_snoopy->submit($submit,$post);
$result = $send_snoopy->results;
if(json_decode($result,1)){
return true;
}else{
return false;
}
}
}