forked from dodgepudding/wechat-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat.class.php
3576 lines (3410 loc) · 119 KB
/
wechat.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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* 微信公众平台PHP-SDK, 官方API部分
* @author dodge <[email protected]>
* @link https://github.com/dodgepudding/wechat-php-sdk
* @version 1.2
* usage:
* $options = array(
* 'token'=>'tokenaccesskey', //填写你设定的key
* 'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
* 'appid'=>'wxdk1234567890', //填写高级调用功能的app id
* 'appsecret'=>'xxxxxxxxxxxxxxxxxxx' //填写高级调用功能的密钥
* );
* $weObj = new Wechat($options);
* $weObj->valid();
* $type = $weObj->getRev()->getRevType();
* switch($type) {
* case Wechat::MSGTYPE_TEXT:
* $weObj->text("hello, I'm wechat")->reply();
* exit;
* break;
* case Wechat::MSGTYPE_EVENT:
* ....
* break;
* case Wechat::MSGTYPE_IMAGE:
* ...
* break;
* default:
* $weObj->text("help info")->reply();
* }
*
* //获取菜单操作:
* $menu = $weObj->getMenu();
* //设置菜单
* $newmenu = array(
* "button"=>
* array(
* array('type'=>'click','name'=>'最新消息','key'=>'MENU_KEY_NEWS'),
* array('type'=>'view','name'=>'我要搜索','url'=>'http://www.baidu.com'),
* )
* );
* $result = $weObj->createMenu($newmenu);
*/
class Wechat
{
const MSGTYPE_TEXT = 'text';
const MSGTYPE_IMAGE = 'image';
const MSGTYPE_LOCATION = 'location';
const MSGTYPE_LINK = 'link';
const MSGTYPE_EVENT = 'event';
const MSGTYPE_MUSIC = 'music';
const MSGTYPE_NEWS = 'news';
const MSGTYPE_VOICE = 'voice';
const MSGTYPE_VIDEO = 'video';
const EVENT_SUBSCRIBE = 'subscribe'; //订阅
const EVENT_UNSUBSCRIBE = 'unsubscribe'; //取消订阅
const EVENT_SCAN = 'SCAN'; //扫描带参数二维码
const EVENT_LOCATION = 'LOCATION'; //上报地理位置
const EVENT_MENU_VIEW = 'VIEW'; //菜单 - 点击菜单跳转链接
const EVENT_MENU_CLICK = 'CLICK'; //菜单 - 点击菜单拉取消息
const EVENT_MENU_SCAN_PUSH = 'scancode_push'; //菜单 - 扫码推事件(客户端跳URL)
const EVENT_MENU_SCAN_WAITMSG = 'scancode_waitmsg'; //菜单 - 扫码推事件(客户端不跳URL)
const EVENT_MENU_PIC_SYS = 'pic_sysphoto'; //菜单 - 弹出系统拍照发图
const EVENT_MENU_PIC_PHOTO = 'pic_photo_or_album'; //菜单 - 弹出拍照或者相册发图
const EVENT_MENU_PIC_WEIXIN = 'pic_weixin'; //菜单 - 弹出微信相册发图器
const EVENT_MENU_LOCATION = 'location_select'; //菜单 - 弹出地理位置选择器
const EVENT_SEND_MASS = 'MASSSENDJOBFINISH'; //发送结果 - 高级群发完成
const EVENT_SEND_TEMPLATE = 'TEMPLATESENDJOBFINISH';//发送结果 - 模板消息发送结果
const EVENT_KF_SEESION_CREATE = 'kfcreatesession'; //多客服 - 接入会话
const EVENT_KF_SEESION_CLOSE = 'kfclosesession'; //多客服 - 关闭会话
const EVENT_KF_SEESION_SWITCH = 'kfswitchsession'; //多客服 - 转接会话
const EVENT_CARD_PASS = 'card_pass_check'; //卡券 - 审核通过
const EVENT_CARD_NOTPASS = 'card_not_pass_check'; //卡券 - 审核未通过
const EVENT_CARD_USER_GET = 'user_get_card'; //卡券 - 用户领取卡券
const EVENT_CARD_USER_DEL = 'user_del_card'; //卡券 - 用户删除卡券
const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
const AUTH_URL = '/token?grant_type=client_credential&';
const MENU_CREATE_URL = '/menu/create?';
const MENU_GET_URL = '/menu/get?';
const MENU_DELETE_URL = '/menu/delete?';
const GET_TICKET_URL = '/ticket/getticket?';
const CALLBACKSERVER_GET_URL = '/getcallbackip?';
const QRCODE_CREATE_URL='/qrcode/create?';
const QR_SCENE = 0;
const QR_LIMIT_SCENE = 1;
const QRCODE_IMG_URL='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=';
const SHORT_URL='/shorturl?';
const USER_GET_URL='/user/get?';
const USER_INFO_URL='/user/info?';
const USER_UPDATEREMARK_URL='/user/info/updateremark?';
const GROUP_GET_URL='/groups/get?';
const USER_GROUP_URL='/groups/getid?';
const GROUP_CREATE_URL='/groups/create?';
const GROUP_UPDATE_URL='/groups/update?';
const GROUP_MEMBER_UPDATE_URL='/groups/members/update?';
const GROUP_MEMBER_BATCHUPDATE_URL='/groups/members/batchupdate?';
const CUSTOM_SEND_URL='/message/custom/send?';
const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?';
const MASS_SEND_URL = '/message/mass/send?';
const TEMPLATE_SET_INDUSTRY_URL = '/message/template/api_set_industry?';
const TEMPLATE_ADD_TPL_URL = '/message/template/api_add_template?';
const TEMPLATE_SEND_URL = '/message/template/send?';
const MASS_SEND_GROUP_URL = '/message/mass/sendall?';
const MASS_DELETE_URL = '/message/mass/delete?';
const MASS_PREVIEW_URL = '/message/mass/preview?';
const MASS_QUERY_URL = '/message/mass/get?';
const UPLOAD_MEDIA_URL = 'http://file.api.weixin.qq.com/cgi-bin';
const MEDIA_UPLOAD_URL = '/media/upload?';
const MEDIA_GET_URL = '/media/get?';
const MEDIA_VIDEO_UPLOAD = '/media/uploadvideo?';
const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
const MEDIA_FOREVER_GET_URL = '/material/get_material?';
const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';
const OAUTH_PREFIX = 'https://open.weixin.qq.com/connect/oauth2';
const OAUTH_AUTHORIZE_URL = '/authorize?';
///多客服相关地址
const CUSTOM_SERVICE_GET_RECORD = '/customservice/getrecord?';
const CUSTOM_SERVICE_GET_KFLIST = '/customservice/getkflist?';
const CUSTOM_SERVICE_GET_ONLINEKFLIST = '/customservice/getonlinekflist?';
const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com'; //以下API接口URL需要使用此前缀
const OAUTH_TOKEN_URL = '/sns/oauth2/access_token?';
const OAUTH_REFRESH_URL = '/sns/oauth2/refresh_token?';
const OAUTH_USERINFO_URL = '/sns/userinfo?';
const OAUTH_AUTH_URL = '/sns/auth?';
///多客服相关地址
const CUSTOM_SESSION_CREATE = '/customservice/kfsession/create?';
const CUSTOM_SESSION_CLOSE = '/customservice/kfsession/close?';
const CUSTOM_SESSION_SWITCH = '/customservice/kfsession/switch?';
const CUSTOM_SESSION_GET = '/customservice/kfsession/getsession?';
const CUSTOM_SESSION_GET_LIST = '/customservice/kfsession/getsessionlist?';
const CUSTOM_SESSION_GET_WAIT = '/customservice/kfsession/getwaitcase?';
const CS_KF_ACCOUNT_ADD_URL = '/customservice/kfaccount/add?';
const CS_KF_ACCOUNT_UPDATE_URL = '/customservice/kfaccount/update?';
const CS_KF_ACCOUNT_DEL_URL = '/customservice/kfaccount/del?';
const CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL = '/customservice/kfaccount/uploadheadimg?';
///卡券相关地址
const CARD_CREATE = '/card/create?';
const CARD_DELETE = '/card/delete?';
const CARD_UPDATE = '/card/update?';
const CARD_GET = '/card/get?';
const CARD_BATCHGET = '/card/batchget?';
const CARD_MODIFY_STOCK = '/card/modifystock?';
const CARD_LOCATION_BATCHADD = '/card/location/batchadd?';
const CARD_LOCATION_BATCHGET = '/card/location/batchget?';
const CARD_GETCOLORS = '/card/getcolors?';
const CARD_QRCODE_CREATE = '/card/qrcode/create?';
const CARD_CODE_CONSUME = '/card/code/consume?';
const CARD_CODE_DECRYPT = '/card/code/decrypt?';
const CARD_CODE_GET = '/card/code/get?';
const CARD_CODE_UPDATE = '/card/code/update?';
const CARD_CODE_UNAVAILABLE = '/card/code/unavailable?';
const CARD_TESTWHILELIST_SET = '/card/testwhitelist/set?';
const CARD_MEMBERCARD_ACTIVATE = '/card/membercard/activate?'; //激活会员卡
const CARD_MEMBERCARD_UPDATEUSER = '/card/membercard/updateuser?'; //更新会员卡
const CARD_MOVIETICKET_UPDATEUSER = '/card/movieticket/updateuser?'; //更新电影票(未加方法)
const CARD_BOARDINGPASS_CHECKIN = '/card/boardingpass/checkin?'; //飞机票-在线选座(未加方法)
const CARD_LUCKYMONEY_UPDATE = '/card/luckymoney/updateuserbalance?'; //更新红包金额
const SEMANTIC_API_URL = '/semantic/semproxy/search?'; //语义理解
///数据分析接口
static $DATACUBE_URL_ARR = array( //用户分析
'user' => array(
'summary' => '/datacube/getusersummary?', //获取用户增减数据(getusersummary)
'cumulate' => '/datacube/getusercumulate?', //获取累计用户数据(getusercumulate)
),
'article' => array( //图文分析
'summary' => '/datacube/getarticlesummary?', //获取图文群发每日数据(getarticlesummary)
'total' => '/datacube/getarticletotal?', //获取图文群发总数据(getarticletotal)
'read' => '/datacube/getuserread?', //获取图文统计数据(getuserread)
'readhour' => '/datacube/getuserreadhour?', //获取图文统计分时数据(getuserreadhour)
'share' => '/datacube/getusershare?', //获取图文分享转发数据(getusershare)
'sharehour' => '/datacube/getusersharehour?', //获取图文分享转发分时数据(getusersharehour)
),
'upstreammsg' => array( //消息分析
'summary' => '/datacube/getupstreammsg?', //获取消息发送概况数据(getupstreammsg)
'hour' => '/datacube/getupstreammsghour?', //获取消息分送分时数据(getupstreammsghour)
'week' => '/datacube/getupstreammsgweek?', //获取消息发送周数据(getupstreammsgweek)
'month' => '/datacube/getupstreammsgmonth?', //获取消息发送月数据(getupstreammsgmonth)
'dist' => '/datacube/getupstreammsgdist?', //获取消息发送分布数据(getupstreammsgdist)
'distweek' => '/datacube/getupstreammsgdistweek?', //获取消息发送分布周数据(getupstreammsgdistweek)
'distmonth' => '/datacube/getupstreammsgdistmonth?', //获取消息发送分布月数据(getupstreammsgdistmonth)
),
'interface' => array( //接口分析
'summary' => '/datacube/getinterfacesummary?', //获取接口分析数据(getinterfacesummary)
'summaryhour' => '/datacube/getinterfacesummaryhour?', //获取接口分析分时数据(getinterfacesummaryhour)
)
);
private $token;
private $encodingAesKey;
private $encrypt_type;
private $appid;
private $appsecret;
private $access_token;
private $jsapi_ticket;
private $user_token;
private $partnerid;
private $partnerkey;
private $paysignkey;
private $postxml;
private $_msg;
private $_funcflag = false;
private $_receive;
private $_text_filter = true;
public $debug = false;
public $errCode = 40001;
public $errMsg = "no access";
public $logcallback;
public function __construct($options)
{
$this->token = isset($options['token'])?$options['token']:'';
$this->encodingAesKey = isset($options['encodingaeskey'])?$options['encodingaeskey']:'';
$this->appid = isset($options['appid'])?$options['appid']:'';
$this->appsecret = isset($options['appsecret'])?$options['appsecret']:'';
$this->debug = isset($options['debug'])?$options['debug']:false;
$this->logcallback = isset($options['logcallback'])?$options['logcallback']:false;
}
/**
* For weixin server validation
*/
private function checkSignature($str='')
{
$signature = isset($_GET["signature"])?$_GET["signature"]:'';
$signature = isset($_GET["msg_signature"])?$_GET["msg_signature"]:$signature; //如果存在加密验证则用加密验证段
$timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
$nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';
$token = $this->token;
$tmpArr = array($token, $timestamp, $nonce,$str);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
/**
* For weixin server validation
* @param bool $return 是否返回
*/
public function valid($return=false)
{
$encryptStr="";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$postStr = file_get_contents("php://input");
$array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"]: '';
if ($this->encrypt_type == 'aes') { //aes加密
$this->log($postStr);
$encryptStr = $array['Encrypt'];
$pc = new Prpcrypt($this->encodingAesKey);
$array = $pc->decrypt($encryptStr,$this->appid);
if (!isset($array[0]) || ($array[0] != 0)) {
if (!$return) {
die('decrypt error!');
} else {
return false;
}
}
$this->postxml = $array[1];
if (!$this->appid)
$this->appid = $array[2];//为了没有appid的订阅号。
} else {
$this->postxml = $postStr;
}
} elseif (isset($_GET["echostr"])) {
$echoStr = $_GET["echostr"];
if ($return) {
if ($this->checkSignature())
return $echoStr;
else
return false;
} else {
if ($this->checkSignature())
die($echoStr);
else
die('no access');
}
}
if (!$this->checkSignature($encryptStr)) {
if ($return)
return false;
else
die('no access');
}
return true;
}
/**
* 设置发送消息
* @param array $msg 消息数组
* @param bool $append 是否在原消息数组追加
*/
public function Message($msg = '',$append = false){
if (is_null($msg)) {
$this->_msg =array();
}elseif (is_array($msg)) {
if ($append)
$this->_msg = array_merge($this->_msg,$msg);
else
$this->_msg = $msg;
return $this->_msg;
} else {
return $this->_msg;
}
}
/**
* 设置消息的星标标志,官方已取消对此功能的支持
*/
public function setFuncFlag($flag) {
$this->_funcflag = $flag;
return $this;
}
/**
* 日志记录,可被重载。
* @param mixed $log 输入日志
* @return mixed
*/
protected 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);
}
}
/**
* 获取微信服务器发来的信息
*/
public function getRev()
{
if ($this->_receive) return $this;
$postStr = !empty($this->postxml)?$this->postxml:file_get_contents("php://input");
//兼顾使用明文又不想调用valid()方法的情况
$this->log($postStr);
if (!empty($postStr)) {
$this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
}
return $this;
}
/**
* 获取微信服务器发来的信息
*/
public function getRevData()
{
return $this->_receive;
}
/**
* 获取消息发送者
*/
public function getRevFrom() {
if (isset($this->_receive['FromUserName']))
return $this->_receive['FromUserName'];
else
return false;
}
/**
* 获取消息接受者
*/
public function getRevTo() {
if (isset($this->_receive['ToUserName']))
return $this->_receive['ToUserName'];
else
return false;
}
/**
* 获取接收消息的类型
*/
public function getRevType() {
if (isset($this->_receive['MsgType']))
return $this->_receive['MsgType'];
else
return false;
}
/**
* 获取消息ID
*/
public function getRevID() {
if (isset($this->_receive['MsgId']))
return $this->_receive['MsgId'];
else
return false;
}
/**
* 获取消息发送时间
*/
public function getRevCtime() {
if (isset($this->_receive['CreateTime']))
return $this->_receive['CreateTime'];
else
return false;
}
/**
* 获取接收消息内容正文
*/
public function getRevContent(){
if (isset($this->_receive['Content']))
return $this->_receive['Content'];
else if (isset($this->_receive['Recognition'])) //获取语音识别文字内容,需申请开通
return $this->_receive['Recognition'];
else
return false;
}
/**
* 获取接收消息图片
*/
public function getRevPic(){
if (isset($this->_receive['PicUrl']))
return array(
'mediaid'=>$this->_receive['MediaId'],
'picurl'=>(string)$this->_receive['PicUrl'], //防止picurl为空导致解析出错
);
else
return false;
}
/**
* 获取接收消息链接
*/
public function getRevLink(){
if (isset($this->_receive['Url'])){
return array(
'url'=>$this->_receive['Url'],
'title'=>$this->_receive['Title'],
'description'=>$this->_receive['Description']
);
} else
return false;
}
/**
* 获取接收地理位置
*/
public function getRevGeo(){
if (isset($this->_receive['Location_X'])){
return array(
'x'=>$this->_receive['Location_X'],
'y'=>$this->_receive['Location_Y'],
'scale'=>$this->_receive['Scale'],
'label'=>$this->_receive['Label']
);
} else
return false;
}
/**
* 获取上报地理位置事件
*/
public function getRevEventGeo(){
if (isset($this->_receive['Latitude'])){
return array(
'x'=>$this->_receive['Latitude'],
'y'=>$this->_receive['Longitude'],
'precision'=>$this->_receive['Precision'],
);
} else
return false;
}
/**
* 获取接收事件推送
*/
public function getRevEvent(){
if (isset($this->_receive['Event'])){
$array['event'] = $this->_receive['Event'];
}
if (isset($this->_receive['EventKey'])){
$array['key'] = $this->_receive['EventKey'];
}
if (isset($array) && count($array) > 0) {
return $array;
} else {
return false;
}
}
/**
* 获取自定义菜单的扫码推事件信息
*
* 事件类型为以下两种时则调用此方法有效
* Event 事件类型,scancode_push
* Event 事件类型,scancode_waitmsg
*
* @return: array | false
* array (
* 'ScanType'=>'qrcode',
* 'ScanResult'=>'123123'
* )
*/
public function getRevScanInfo(){
if (isset($this->_receive['ScanCodeInfo'])){
if (!is_array($this->_receive['ScanCodeInfo'])) {
$array=(array)$this->_receive['ScanCodeInfo'];
$this->_receive['ScanCodeInfo']=$array;
}else {
$array=$this->_receive['ScanCodeInfo'];
}
}
if (isset($array) && count($array) > 0) {
return $array;
} else {
return false;
}
}
/**
* 获取自定义菜单的图片发送事件信息
*
* 事件类型为以下三种时则调用此方法有效
* Event 事件类型,pic_sysphoto 弹出系统拍照发图的事件推送
* Event 事件类型,pic_photo_or_album 弹出拍照或者相册发图的事件推送
* Event 事件类型,pic_weixin 弹出微信相册发图器的事件推送
*
* @return: array | false
* array (
* 'Count' => '2',
* 'PicList' =>array (
* 'item' =>array (
* 0 =>array ('PicMd5Sum' => 'aaae42617cf2a14342d96005af53624c'),
* 1 =>array ('PicMd5Sum' => '149bd39e296860a2adc2f1bb81616ff8'),
* ),
* ),
* )
*
*/
public function getRevSendPicsInfo(){
if (isset($this->_receive['SendPicsInfo'])){
if (!is_array($this->_receive['SendPicsInfo'])) {
$array=(array)$this->_receive['SendPicsInfo'];
if (isset($array['PicList'])){
$array['PicList']=(array)$array['PicList'];
$item=$array['PicList']['item'];
$array['PicList']['item']=array();
foreach ( $item as $key => $value ){
$array['PicList']['item'][$key]=(array)$value;
}
}
$this->_receive['SendPicsInfo']=$array;
} else {
$array=$this->_receive['SendPicsInfo'];
}
}
if (isset($array) && count($array) > 0) {
return $array;
} else {
return false;
}
}
/**
* 获取自定义菜单的地理位置选择器事件推送
*
* 事件类型为以下时则可以调用此方法有效
* Event 事件类型,location_select 弹出地理位置选择器的事件推送
*
* @return: array | false
* array (
* 'Location_X' => '33.731655000061',
* 'Location_Y' => '113.29955200008047',
* 'Scale' => '16',
* 'Label' => '某某市某某区某某路',
* 'Poiname' => '',
* )
*
*/
public function getRevSendGeoInfo(){
if (isset($this->_receive['SendLocationInfo'])){
if (!is_array($this->_receive['SendLocationInfo'])) {
$array=(array)$this->_receive['SendLocationInfo'];
if (empty($array['Poiname'])) {
$array['Poiname']="";
}
if (empty($array['Label'])) {
$array['Label']="";
}
$this->_receive['SendLocationInfo']=$array;
} else {
$array=$this->_receive['SendLocationInfo'];
}
}
if (isset($array) && count($array) > 0) {
return $array;
} else {
return false;
}
}
/**
* 获取接收语音推送
*/
public function getRevVoice(){
if (isset($this->_receive['MediaId'])){
return array(
'mediaid'=>$this->_receive['MediaId'],
'format'=>$this->_receive['Format'],
);
} else
return false;
}
/**
* 获取接收视频推送
*/
public function getRevVideo(){
if (isset($this->_receive['MediaId'])){
return array(
'mediaid'=>$this->_receive['MediaId'],
'thumbmediaid'=>$this->_receive['ThumbMediaId']
);
} else
return false;
}
/**
* 获取接收TICKET
*/
public function getRevTicket(){
if (isset($this->_receive['Ticket'])){
return $this->_receive['Ticket'];
} else
return false;
}
/**
* 获取二维码的场景值
*/
public function getRevSceneId (){
if (isset($this->_receive['EventKey'])){
return str_replace('qrscene_','',$this->_receive['EventKey']);
} else{
return false;
}
}
/**
* 获取主动推送的消息ID
* 经过验证,这个和普通的消息MsgId不一样
* 当Event为 MASSSENDJOBFINISH 或 TEMPLATESENDJOBFINISH
*/
public function getRevTplMsgID(){
if (isset($this->_receive['MsgID'])){
return $this->_receive['MsgID'];
} else
return false;
}
/**
* 获取模板消息发送状态
*/
public function getRevStatus(){
if (isset($this->_receive['Status'])){
return $this->_receive['Status'];
} else
return false;
}
/**
* 获取群发或模板消息发送结果
* 当Event为 MASSSENDJOBFINISH 或 TEMPLATESENDJOBFINISH,即高级群发/模板消息
*/
public function getRevResult(){
if (isset($this->_receive['Status'])) //发送是否成功,具体的返回值请参考 高级群发/模板消息 的事件推送说明
$array['Status'] = $this->_receive['Status'];
if (isset($this->_receive['MsgID'])) //发送的消息id
$array['MsgID'] = $this->_receive['MsgID'];
//以下仅当群发消息时才会有的事件内容
if (isset($this->_receive['TotalCount'])) //分组或openid列表内粉丝数量
$array['TotalCount'] = $this->_receive['TotalCount'];
if (isset($this->_receive['FilterCount'])) //过滤(过滤是指特定地区、性别的过滤、用户设置拒收的过滤,用户接收已超4条的过滤)后,准备发送的粉丝数
$array['FilterCount'] = $this->_receive['FilterCount'];
if (isset($this->_receive['SentCount'])) //发送成功的粉丝数
$array['SentCount'] = $this->_receive['SentCount'];
if (isset($this->_receive['ErrorCount'])) //发送失败的粉丝数
$array['ErrorCount'] = $this->_receive['ErrorCount'];
if (isset($array) && count($array) > 0) {
return $array;
} else {
return false;
}
}
/**
* 获取多客服会话状态推送事件 - 接入会话
* 当Event为 kfcreatesession 即接入会话
* @return string | boolean 返回分配到的客服
*/
public function getRevKFCreate(){
if (isset($this->_receive['KfAccount'])){
return $this->_receive['KfAccount'];
} else
return false;
}
/**
* 获取多客服会话状态推送事件 - 关闭会话
* 当Event为 kfclosesession 即关闭会话
* @return string | boolean 返回分配到的客服
*/
public function getRevKFClose(){
if (isset($this->_receive['KfAccount'])){
return $this->_receive['KfAccount'];
} else
return false;
}
/**
* 获取多客服会话状态推送事件 - 转接会话
* 当Event为 kfswitchsession 即转接会话
* @return array | boolean 返回分配到的客服
* {
* 'FromKfAccount' => '', //原接入客服
* 'ToKfAccount' => '' //转接到客服
* }
*/
public function getRevKFSwitch(){
if (isset($this->_receive['FromKfAccount'])) //原接入客服
$array['FromKfAccount'] = $this->_receive['FromKfAccount'];
if (isset($this->_receive['ToKfAccount'])) //转接到客服
$array['ToKfAccount'] = $this->_receive['ToKfAccount'];
if (isset($array) && count($array) > 0) {
return $array;
} else {
return false;
}
}
/**
* 获取卡券事件推送 - 卡卷审核是否通过
* 当Event为 card_pass_check(审核通过) 或 card_not_pass_check(未通过)
* @return string|boolean 返回卡券ID
*/
public function getRevCardPass(){
if (isset($this->_receive['CardId']))
return $this->_receive['CardId'];
else
return false;
}
/**
* 获取卡券事件推送 - 领取卡券
* 当Event为 user_get_card(用户领取卡券)
* @return array|boolean
*/
public function getRevCardGet(){
if (isset($this->_receive['CardId'])) //卡券 ID
$array['CardId'] = $this->_receive['CardId'];
if (isset($this->_receive['IsGiveByFriend'])) //是否为转赠,1 代表是,0 代表否。
$array['IsGiveByFriend'] = $this->_receive['IsGiveByFriend'];
if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode'])) //code 序列号。自定义 code 及非自定义 code的卡券被领取后都支持事件推送。
$array['UserCardCode'] = $this->_receive['UserCardCode'];
if (isset($array) && count($array) > 0) {
return $array;
} else {
return false;
}
}
/**
* 获取卡券事件推送 - 删除卡券
* 当Event为 user_del_card(用户删除卡券)
* @return array|boolean
*/
public function getRevCardDel(){
if (isset($this->_receive['CardId'])) //卡券 ID
$array['CardId'] = $this->_receive['CardId'];
if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode'])) //code 序列号。自定义 code 及非自定义 code的卡券被领取后都支持事件推送。
$array['UserCardCode'] = $this->_receive['UserCardCode'];
if (isset($array) && count($array) > 0) {
return $array;
} else {
return false;
}
}
public static function xmlSafeStr($str)
{
return '<![CDATA['.preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/",'',$str).']]>';
}
/**
* 数据XML编码
* @param mixed $data 数据
* @return string
*/
public static function data_to_xml($data) {
$xml = '';
foreach ($data as $key => $val) {
is_numeric($key) && $key = "item id=\"$key\"";
$xml .= "<$key>";
$xml .= ( is_array($val) || is_object($val)) ? self::data_to_xml($val) : self::xmlSafeStr($val);
list($key, ) = explode(' ', $key);
$xml .= "</$key>";
}
return $xml;
}
/**
* XML编码
* @param mixed $data 数据
* @param string $root 根节点名
* @param string $item 数字索引的子节点名
* @param string $attr 根节点属性
* @param string $id 数字索引子节点key转换的属性名
* @param string $encoding 数据编码
* @return string
*/
public function xml_encode($data, $root='xml', $item='item', $attr='', $id='id', $encoding='utf-8') {
if(is_array($attr)){
$_attr = array();
foreach ($attr as $key => $value) {
$_attr[] = "{$key}=\"{$value}\"";
}
$attr = implode(' ', $_attr);
}
$attr = trim($attr);
$attr = empty($attr) ? '' : " {$attr}";
$xml = "<{$root}{$attr}>";
$xml .= self::data_to_xml($data, $item, $id);
$xml .= "</{$root}>";
return $xml;
}
/**
* 过滤文字回复\r\n换行符
* @param string $text
* @return string|mixed
*/
private function _auto_text_filter($text) {
if (!$this->_text_filter) return $text;
return str_replace("\r\n", "\n", $text);
}
/**
* 设置回复消息
* Example: $obj->text('hello')->reply();
* @param string $text
*/
public function text($text='')
{
$FuncFlag = $this->_funcflag ? 1 : 0;
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName'=>$this->getRevTo(),
'MsgType'=>self::MSGTYPE_TEXT,
'Content'=>$this->_auto_text_filter($text),
'CreateTime'=>time(),
'FuncFlag'=>$FuncFlag
);
$this->Message($msg);
return $this;
}
/**
* 设置回复消息
* Example: $obj->image('media_id')->reply();
* @param string $mediaid
*/
public function image($mediaid='')
{
$FuncFlag = $this->_funcflag ? 1 : 0;
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName'=>$this->getRevTo(),
'MsgType'=>self::MSGTYPE_IMAGE,
'Image'=>array('MediaId'=>$mediaid),
'CreateTime'=>time(),
'FuncFlag'=>$FuncFlag
);
$this->Message($msg);
return $this;
}
/**
* 设置回复消息
* Example: $obj->voice('media_id')->reply();
* @param string $mediaid
*/
public function voice($mediaid='')
{
$FuncFlag = $this->_funcflag ? 1 : 0;
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName'=>$this->getRevTo(),
'MsgType'=>self::MSGTYPE_VOICE,
'Voice'=>array('MediaId'=>$mediaid),
'CreateTime'=>time(),
'FuncFlag'=>$FuncFlag
);
$this->Message($msg);
return $this;
}
/**
* 设置回复消息
* Example: $obj->video('media_id','title','description')->reply();
* @param string $mediaid
*/
public function video($mediaid='',$title='',$description='')
{
$FuncFlag = $this->_funcflag ? 1 : 0;
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName'=>$this->getRevTo(),
'MsgType'=>self::MSGTYPE_VIDEO,
'Video'=>array(
'MediaId'=>$mediaid,
'Title'=>$title,
'Description'=>$description
),
'CreateTime'=>time(),
'FuncFlag'=>$FuncFlag
);
$this->Message($msg);
return $this;
}
/**
* 设置回复音乐
* @param string $title
* @param string $desc
* @param string $musicurl
* @param string $hgmusicurl
* @param string $thumbmediaid 音乐图片缩略图的媒体id,非必须
*/
public function music($title,$desc,$musicurl,$hgmusicurl='',$thumbmediaid='') {
$FuncFlag = $this->_funcflag ? 1 : 0;
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName'=>$this->getRevTo(),
'CreateTime'=>time(),
'MsgType'=>self::MSGTYPE_MUSIC,
'Music'=>array(
'Title'=>$title,
'Description'=>$desc,
'MusicUrl'=>$musicurl,
'HQMusicUrl'=>$hgmusicurl
),
'FuncFlag'=>$FuncFlag
);
if ($thumbmediaid) {
$msg['Music']['ThumbMediaId'] = $thumbmediaid;
}
$this->Message($msg);
return $this;
}
/**
* 设置回复图文
* @param array $newsData
* 数组结构:
* array(
* "0"=>array(
* 'Title'=>'msg title',
* 'Description'=>'summary text',
* 'PicUrl'=>'http://www.domain.com/1.jpg',
* 'Url'=>'http://www.domain.com/1.html'
* ),
* "1"=>....
* )
*/
public function news($newsData=array())
{
$FuncFlag = $this->_funcflag ? 1 : 0;
$count = count($newsData);
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName'=>$this->getRevTo(),
'MsgType'=>self::MSGTYPE_NEWS,
'CreateTime'=>time(),
'ArticleCount'=>$count,
'Articles'=>$newsData,
'FuncFlag'=>$FuncFlag
);
$this->Message($msg);
return $this;
}
/**
*