forked from xjchengo/wechat-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechatext.class.php
713 lines (675 loc) · 24.3 KB
/
wechatext.class.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
<?php
/**
* 微信公众平台PHP-SDK
* Wechatext为非官方微信发送API
* 注: 用户id为通过getMsg()方法获取的FakeId值
* 主要实现如下功能:
* send($id,$content) 向某用户id发送微信文字信息
* getUserList($page,$pagesize,$group) 获取用户信息
* getGroupList($page,$pagesize) 获取群组信息
* sendNews($id,$msgid) 发送图文消息
* getNewsList($page,$pagesize) 获取图文信息列表
* uploadFile($filepath,$type) 上传附件,包括图片/音频/视频
* addPreview($title,$author,$summary,$content,$photoid,$srcurl='') 创建新的图文信息
* getFileList($type,$page,$pagesize) 获取素材库文件列表
* sendImage($id,$fid) 发送图片消息
* sendAudio($id,$fid) 发送音频消息
* sendVideo($id,$fid) 发送视频消息
* getInfo($id) 根据id获取用户资料
* getNewMsgNum($lastid) 获取从$lastid算起新消息的数目
* getTopMsg() 获取最新一条消息的数据, 此方法获取的消息id可以作为检测新消息的$lastid依据
* getMsg($lastid,$offset=0,$perpage=50,$day=0,$today=0,$star=0) 获取最新的消息列表, 列表将返回消息id, 用户id, 消息类型, 文字消息等参数
* 消息返回结构: {"id":"消息id","type":"类型号(1为文字,2为图片,3为语音)","fileId":"0","hasReply":"0","fakeId":"用户uid","nickName":"昵称","dateTime":"时间戳","content":"文字内容"}
* getMsgImage($msgid,$mode='large') 若消息type类型为2, 调用此方法获取图片数据
* getMsgVoice($msgid) 若消息type类型为3, 调用此方法获取语音数据
* @author dodge <[email protected]>
* @link https://github.com/dodgepudding/wechat-php-sdk
* @version 1.2
*
*/
include "snoopy.class.php";
class Wechatext
{
private $cookie;
private $_cookiename;
private $_cookieexpired = 3600;
private $_account;
private $_password;
private $_datapath = './data/cookie_';
private $debug;
private $_logcallback;
private $_token;
public function __construct($options)
{
$this->_account = isset($options['account'])?$options['account']:'';
$this->_password = isset($options['password'])?$options['password']:'';
$this->_datapath = isset($options['datapath'])?$options['datapath']:$this->_datapath;
$this->debug = isset($options['debug'])?$options['debug']:false;
$this->_logcallback = isset($options['logcallback'])?$options['logcallback']:false;
$this->_cookiename = $this->_datapath.$this->_account;
$this->cookie = $this->getCookie($this->_cookiename);
}
/**
* 主动发消息
* @param string $id 用户的uid(即FakeId)
* @param string $content 发送的内容
*/
public function send($id,$content)
{
$send_snoopy = new Snoopy;
$post = array();
$post['tofakeid'] = $id;
$post['type'] = 1;
$post['token'] = $this->_token;
$post['content'] = $content;
$post['ajax'] = 1;
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=$id&token={$this->_token}&lang=zh_CN";
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
$send_snoopy->submit($submit,$post);
$this->log($send_snoopy->results);
return $send_snoopy->results;
}
/**
* 群发功能 纯文本
* @param string $content
* @return string
*/
public function mass($content) {
$send_snoopy = new Snoopy;
$post = array();
$post['type'] = 1;
$post['token'] = $this->_token;
$post['content'] = $content;
$post['ajax'] = 1;
$post['city']='';
$post['country']='';
$post['f']='json';
$post['groupid']='-1';
$post['imgcode']='';
$post['lang']='zh_CN';
$post['province']='';
$post['random']= rand(0, 1);
$post['sex']=0;
$post['synctxnews']=0;
$post['synctxweibo']=0;
$post['t']='ajax-response';
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token={$this->_token}&lang=zh_CN";
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "https://mp.weixin.qq.com/cgi-bin/masssend";
$send_snoopy->submit($submit,$post);
$this->log($send_snoopy->results);
return $send_snoopy->results;
}
/**
* 群发功能 图文素材
* @param int $appmsgid 图文素材ID
* @return string
*/
function massNews($appmsgid){
$send_snoopy = new Snoopy;
$post = array();
$post['type'] = 10;
$post['token'] = $this->_token;
$post['appmsgid'] = $appmsgid;
$post['ajax'] = 1;
$post['city']='';
$post['country']='';
$post['f']='json';
$post['groupid']='-1';
$post['imgcode']='';
$post['lang']='zh_CN';
$post['province']='';
$post['random']= rand(0, 1);
$post['sex']=0;
$post['synctxnews']=0;
$post['synctxweibo']=0;
$post['t']='ajax-response';
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token={$this->_token}&lang=zh_CN";
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "https://mp.weixin.qq.com/cgi-bin/masssend";
$send_snoopy->submit($submit,$post);
$this->log($send_snoopy->results);
return $send_snoopy->results;
}
/**
* 获取用户列表列表
* @param $page 页码(从0开始)
* @param $pagesize 每页大小
* @param $groupid 分组id
* @return array ({contacts:[{id:12345667,nick_name:"昵称",remark_name:"备注名",group_id:0},{}....]})
*/
function getUserList($page=0,$pagesize=10,$groupid=0){
$send_snoopy = new Snoopy;
$t = time().strval(mt_rand(100,999));
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=".$pagesize."&pageidx=".$page."&type=0&groupid=0&lang=zh_CN&token=".$this->_token;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=".$pagesize."&pageidx=".$page."&type=0&groupid=$groupid&lang=zh_CN&f=json&token=".$this->_token;
$send_snoopy->fetch($submit);
$result = $send_snoopy->results;
$this->log('userlist:'.$result);
$json = json_decode($result,true);
if (isset($json['contact_list'])) {
$json = json_decode($json['contact_list'],true);
if (isset($json['contacts']))
return $json['contacts'];
}
return false;
}
/**
* 获取分组列表
*
*/
function getGroupList(){
$send_snoopy = new Snoopy;
$t = time().strval(mt_rand(100,999));
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=10&pageidx=0&type=0&groupid=0&lang=zh_CN&token=".$this->_token;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=10&pageidx=0&type=0&groupid=0&lang=zh_CN&f=json&token=".$this->_token;
$send_snoopy->fetch($submit);
$result = $send_snoopy->results;
$this->log('userlist:'.$result);
$json = json_decode($result,true);
if (isset($json['group_list'])){
$json = json_decode($json['group_list'],true);
if (isset($json['groups']))
return $json['groups'];
}
return false;
}
/**
* 获取图文信息列表
* @param $page 页码(从0开始)
* @param $pagesize 每页大小
* @return array
*/
public function getNewsList($page,$pagesize=10) {
$send_snoopy = new Snoopy;
$t = time().strval(mt_rand(100,999));
$type=10;
$begin = $page*$pagesize;
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token=".$this->_token."&lang=zh_CN";
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "https://mp.weixin.qq.com/cgi-bin/appmsg?token=".$this->_token."&lang=zh_CN&type=$type&action=list&begin=$begin&count=$pagesize&f=json&random=0.".$t;
$send_snoopy->fetch($submit);
$result = $send_snoopy->results;
$this->log('newslist:'.$result);
$json = json_decode($result,true);
if (isset($json['app_msg_info'])) {
return $json['app_msg_info'];
}
return false;
}
/**
* 获取与指定用户的对话内容
* @param $fakeid
* @return array
*/
public function getDialogMsg($fakeid) {
$send_snoopy = new Snoopy;
$t = time().strval(mt_rand(100,999));
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token=".$this->_token."&lang=zh_CN";
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=".$fakeid."&token=".$this->_token."&lang=zh_CN&f=json&random=".$t;
$send_snoopy->fetch($submit);
$result = $send_snoopy->results;
$this->log('DialogMsg:'.$result);
$json = json_decode($result,true);
if (isset($json['page_info'])) {
return $json['page_info'];
}
return false;
}
/**
* 发送图文信息,必须从图文库里选取消息ID发送
* @param string $id 用户的uid(即FakeId)
* @param string $msgid 图文消息id
*/
public function sendNews($id,$msgid)
{
$send_snoopy = new Snoopy;
$post = array();
$post['tofakeid'] = $id;
$post['type'] = 10;
$post['token'] = $this->_token;
$post['fid'] = $msgid;
$post['appmsgid'] = $msgid;
$post['error'] = 'false';
$post['ajax'] = 1;
$send_snoopy->referer = "https://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 = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
$send_snoopy->submit($submit,$post);
$this->log($send_snoopy->results);
return $send_snoopy->results;
}
/**
* 上传附件(图片/音频/视频)
* @param string $filepath 本地文件地址
* @param int $type 文件类型: 2:图片 3:音频 4:视频
*/
public function uploadFile($filepath,$type=2) {
$send_snoopy = new Snoopy;
$send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-upload&lang=zh_CN&type=2&formId=1";
$t = time().strval(mt_rand(100,999));
$post = array('formId'=>'');
$postfile = array('uploadfile'=>$filepath);
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$send_snoopy->set_submit_multipart();
$submit = "http://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=$type&token=".$this->_token."&t=iframe-uploadfile&lang=zh_CN&formId= file_from_".$t;
$send_snoopy->submit($submit,$post,$postfile);
$tmp = $send_snoopy->results;
$this->log('upload:'.$tmp);
preg_match("/formId,.*?\'(\d+)\'/",$tmp,$matches);
if (isset($matches[1])) {
return $matches[1];
}
return false;
}
/**
* 创建图文消息
* @param array $title 标题
* @param array $summary 摘要
* @param array $content 内容
* @param array $photoid 素材库里的图片id(可通过uploadFile上传后获取)
* @param array $srcurl 原文链接
* @return json
*/
public function addPreview($title,$author,$summary,$content,$photoid,$srcurl='') {
$send_snoopy = new Snoopy;
$send_snoopy->referer = 'https://mp.weixin.qq.com/cgi-bin/operate_appmsg?lang=zh_CN&sub=edit&t=wxm-appmsgs-edit-new&type=10&subtype=3&token='.$this->_token;
$submit = "https://mp.weixin.qq.com/cgi-bin/operate_appmsg?lang=zh_CN&t=ajax-response&sub=create&token=".$this->_token;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$send_snoopy->set_submit_normal();
$post = array(
'token'=>$this->_token,
'type'=>10,
'lang'=>'zh_CN',
'sub'=>'create',
'ajax'=>1,
'AppMsgId'=>'',
'error'=>'false',
);
if (count($title)==count($author)&&count($title)==count($summary)&&count($title)==count($content)&&count($title)==count($photoid))
{
$i = 0;
foreach($title as $v) {
$post['title'.$i] = $title[$i];
$post['author'.$i] = $author[$i];
$post['digest'.$i] = $summary[$i];
$post['content'.$i] = $content[$i];
$post['fileid'.$i] = $photoid[$i];
if ($srcurl[$i]) $post['sourceurl'.$i] = $srcurl[$i];
$i++;
}
}
$post['count'] = $i;
$post['token'] = $this->_token;
$send_snoopy->submit($submit,$post);
$tmp = $send_snoopy->results;
$this->log('step2:'.$tmp);
$json = json_decode($tmp,true);
return $json;
}
/**
* 发送媒体文件
* @param $id 用户的uid(即FakeId)
* @param $fid 文件id
* @param $type 文件类型
*/
public function sendFile($id,$fid,$type) {
$send_snoopy = new Snoopy;
$post = array();
$post['tofakeid'] = $id;
$post['type'] = $type;
$post['token'] = $this->_token;
$post['fid'] = $fid;
$post['fileid'] = $fid;
$post['error'] = 'false';
$post['ajax'] = 1;
$send_snoopy->referer = "https://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 = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
$send_snoopy->submit($submit,$post);
$result = $send_snoopy->results;
$this->log('sendfile:'.$result);
$json = json_decode($result,true);
if ($json && $json['ret']==0)
return true;
else
return false;
}
/**
* 获取素材库文件列表
* @param $type 文件类型: 2:图片 3:音频 4:视频
* @param $page 页码(从0开始)
* @param $pagesize 每页大小
* @return array
*/
public function getFileList($type,$page,$pagesize=10) {
$send_snoopy = new Snoopy;
$t = time().strval(mt_rand(100,999));
$begin = $page*$pagesize;
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token=".$this->_token."&lang=zh_CN";
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$submit = "https://mp.weixin.qq.com/cgi-bin/filepage?token=".$this->_token."&lang=zh_CN&type=$type&random=0.".$t."&begin=$begin&count=$pagesize&f=json";
$send_snoopy->fetch($submit);
$result = $send_snoopy->results;
$this->log('filelist:'.$result);
$json = json_decode($result,true);
if (isset($json['page_info']))
return $json['page_info'];
else
return false;
}
/**
* 发送图文信息,必须从库里选取文件ID发送
* @param string $id 用户的uid(即FakeId)
* @param string $fid 文件id
*/
public function sendImage($id,$fid)
{
return $this->sendFile($id,$fid,2);
}
/**
* 发送语音信息,必须从库里选取文件ID发送
* @param string $id 用户的uid(即FakeId)
* @param string $fid 语音文件id
*/
public function sendAudio($id,$fid)
{
return $this->sendFile($id,$fid,3);
}
/**
* 发送视频信息,必须从库里选取文件ID发送
* @param string $id 用户的uid(即FakeId)
* @param string $fid 视频文件id
*/
public function sendVideo($id,$fid)
{
return $this->sendFile($id,$fid,4);
}
/**
* 发送预览图文消息
* @param string $account 账户名称(user_name)
* @param string $title 标题
* @param string $summary 摘要
* @param string $content 内容
* @param string $photoid 素材库里的图片id(可通过uploadFile上传后获取)
* @param string $srcurl 原文链接
* @return json
*/
public function sendPreview($account,$title,$summary,$content,$photoid,$srcurl='') {
$send_snoopy = new Snoopy;
$submit = "https://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=preview&t=ajax-appmsg-preview";
$send_snoopy->set_submit_normal();
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$send_snoopy->referer = 'https://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=edit&t=wxm-appmsgs-edit-new&type=10&subtype=3&lang=zh_CN';
$post = array(
'AppMsgId'=>'',
'ajax'=>1,
'content0'=>$content,
'count'=>1,
'digest0'=>$summary,
'error'=>'false',
'fileid0'=>$photoid,
'preusername'=>$account,
'sourceurl0'=>$srcurl,
'title0'=>$title,
);
$post['token'] = $this->_token;
$send_snoopy->submit($submit,$post);
$tmp = $send_snoopy->results;
$this->log('sendpreview:'.$tmp);
$json = json_decode($tmp,true);
return $json;
}
/**
* 获取用户的信息
* @param string $id 用户的uid(即FakeId)
* @return array {fake_id:100001,nick_name:'昵称',user_name:'用户名',signature:'签名档',country:'中国',province:'广东',city:'广州',gender:'1',group_id:'0'},groups:{[id:0,name:'未分组',cnt:20]}
*/
public function getInfo($id)
{
$send_snoopy = new Snoopy;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$t = time().strval(mt_rand(100,999));
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
$submit = "https://mp.weixin.qq.com/cgi-bin/getcontactinfo";
$post = array('ajax'=>1,'lang'=>'zh_CN','random'=>'0.'.$t,'token'=>$this->_token,'t'=>'ajax-getcontactinfo','fakeid'=>$id);
$send_snoopy->submit($submit,$post);
$this->log($send_snoopy->results);
$result = json_decode($send_snoopy->results,true);
if(isset($result['contact_info'])){
return $result['contact_info'];
}
return false;
}
/**
* 获得头像数据
*
* @param FakeId $fakeid
* @return JPG二进制数据
*/
public function getHeadImg($fakeid){
$send_snoopy = new Snoopy;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
$url = "https://mp.weixin.qq.com/cgi-bin/getheadimg?fakeid=$fakeid&token=".$this->_token."&lang=zh_CN";
$send_snoopy->fetch($url);
$result = $send_snoopy->results;
$this->log('Head image:'.$fakeid.'; length:'.strlen($result));
if(!$result){
return false;
}
return $result;
}
/**
* 获取消息更新数目
* @param int $lastid 最近获取的消息ID,为0时获取总消息数目
* @return int 数目
*/
public function getNewMsgNum($lastid=0){
$send_snoopy = new Snoopy;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
$submit = "https://mp.weixin.qq.com/cgi-bin/getnewmsgnum?t=ajax-getmsgnum&lastmsgid=".$lastid;
$post = array('ajax'=>1,'token'=>$this->_token);
$send_snoopy->submit($submit,$post);
$this->log($send_snoopy->results);
$result = json_decode($send_snoopy->results,1);
if(!$result){
return false;
}
return intval($result['newTotalMsgCount']);
}
/**
* 获取最新一条消息
* @return array {"id":"最新一条id","type":"类型号(1为文字,2为图片,3为语音)","fileId":"0","hasReply":"0","fakeId":"用户uid","nickName":"昵称","dateTime":"时间戳","content":"文字内容","playLength":"0","length":"0","source":"","starred":"0","status":"4"}
*/
public function getTopMsg(){
$send_snoopy = new Snoopy;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&lang=zh_CN&token=".$this->_token;
$submit = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&f=json&count=20&day=7&lang=zh_CN&token=".$this->_token;
$send_snoopy->fetch($submit);
$this->log($send_snoopy->results);
$result = $send_snoopy->results;
$json = json_decode($result,true);
if (isset($json['msg_items'])) {
$json = json_decode($json['msg_items'],true);
if(isset($json['msg_item']))
return array_shift($json['msg_item']);
}
return false;
}
/**
* 获取新消息
* @param $lastid 传入最后的消息id编号,为0则从最新一条起倒序获取
* @param $offset lastid起算第一条的偏移量
* @param $perpage 每页获取多少条
* @param $day 最近几天消息(0:今天,1:昨天,2:前天,3:更早,7:五天内)
* @param $today 是否只显示今天的消息, 与$day参数不能同时大于0
* @param $star 是否星标组信息
* @return array[] 同getTopMsg()返回的字段结构相同
*/
public function getMsg($lastid=0,$offset=0,$perpage=20,$day=7,$today=0,$star=0){
$send_snoopy = new Snoopy;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&lang=zh_CN&count=50&token=".$this->_token;
$lastid = $lastid===0 ? '':$lastid;
$addstar = $star?'&action=star':'';
$submit = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&f=json&lang=zh_CN{$addstar}&count=$perpage&timeline=$today&day=$day&frommsgid=$lastid&offset=$offset&token=".$this->_token;
$send_snoopy->fetch($submit);
$this->log($send_snoopy->results);
$result = $send_snoopy->results;
$json = json_decode($result,true);
if (isset($json['msg_items'])) {
$json = json_decode($json['msg_items'],true);
if(isset($json['msg_item']))
return $json['msg_item'];
}
return false;
}
/**
* 获取图片消息
* @param int $msgid 消息id
* @param string $mode 图片尺寸(large/small)
* @return jpg二进制文件
*/
public function getMsgImage($msgid,$mode='large'){
$send_snoopy = new Snoopy;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
$url = "https://mp.weixin.qq.com/cgi-bin/getimgdata?token=".$this->_token."&msgid=$msgid&mode=$mode&source=&fileId=0";
$send_snoopy->fetch($url);
$result = $send_snoopy->results;
$this->log('msg image:'.$msgid.';length:'.strlen($result));
if(!$result){
return false;
}
return $result;
}
/**
* 获取语音消息
* @param int $msgid 消息id
* @return mp3二进制文件
*/
public function getMsgVoice($msgid){
$send_snoopy = new Snoopy;
$send_snoopy->rawheaders['Cookie']= $this->cookie;
$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
$url = "https://mp.weixin.qq.com/cgi-bin/getvoicedata?token=".$this->_token."&msgid=$msgid&fileId=0";
$send_snoopy->fetch($url);
$result = $send_snoopy->results;
$this->log('msg voice:'.$msgid.';length:'.strlen($result));
if(!$result){
return false;
}
return $result;
}
/**
* 模拟登录获取cookie
* @return [type] [description]
*/
/**
* 模拟登录获取cookie
* @return [type] [description]
*/
public function login(){
$snoopy = new Snoopy;
$submit = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
$post["username"] = $this->_account;
$post["pwd"] = md5($this->_password);
$post["f"] = "json";
$post["imgcode"] = "";
$snoopy->referer = "https://mp.weixin.qq.com/";
$snoopy->submit($submit,$post);
$cookie = '';
$this->log($snoopy->results);
$result = json_decode($snoopy->results,true);
if (!isset($result['base_resp']) || $result['base_resp']['ret'] != 0) {
return false;
}
foreach ($snoopy->headers as $key => $value) {
$value = trim($value);
if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $value,$match))
$cookie .=$match[1].'='.$match[2].'; ';
}
preg_match("/token=(\d+)/i",$result['redirect_url'],$matches);
if($matches){
$this->_token = $matches[1];
$this->log('token:'.$this->_token);
}
$this->saveCookie($this->_cookiename,$cookie);
return $cookie;
}
/**
* 把cookie写入缓存
* @param string $filename 缓存文件名
* @param string $content 文件内容
* @return bool
*/
public function saveCookie($filename,$content){
return file_put_contents($filename,$content);
}
/**
* 读取cookie缓存内容
* @param string $filename 缓存文件名
* @return string cookie
*/
public function getCookie($filename){
if (file_exists($filename)) {
$mtime = filemtime($filename);
if ($mtime<time()-$this->_cookieexpired)
$data = '';
else
$data = file_get_contents($filename);
} else
$data = '';
if($data){
$send_snoopy = new Snoopy;
$send_snoopy->rawheaders['Cookie']= $data;
$send_snoopy->maxredirs = 0;
$url = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
$send_snoopy->fetch($url);
$header = implode(',',$send_snoopy->headers);
$this->log('header:'.print_r($send_snoopy->headers,true));
preg_match("/token=(\d+)/i",$header,$matches);
if(empty($matches)){
return $this->login();
}else{
$this->_token = $matches[1];
$this->log('token:'.$this->_token);
return $data;
}
}else{
return $this->login();
}
}
/**
* 验证cookie的有效性
* @return bool
*/
public function checkValid()
{
if (!$this->cookie || !$this->_token) return false;
$send_snoopy = new Snoopy;
$post = array('ajax'=>1,'token'=>$this->_token);
$submit = "https://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;
}
}
private function log($log){
if ($this->debug && function_exists($this->_logcallback)) {
if (is_array($log)) $log = print_r($log,true);
return call_user_func($this->_logcallback,$log);
}
}
}