-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweixin.php
722 lines (695 loc) · 22.9 KB
/
weixin.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
<?php
/*
Plugin Name: Weixin Assistant
Version: 0.0.1
Plugin URI: http://www.imdevice.com
Description: 微信助手
Author: Lu Chunwei
Author URI: http://www.imdevice.com
*/
define("TOKEN","imdevice2weixin");
/*
$wechat=new wechatCallbackApi();
$wechat->valid();
$wechat->respondMsg(); */
class WX_MSG_TYPE{
public static $text="text";
public static $image="image";
public static $voice="voice";
public static $video="video";
public static $location="location";
public static $link="link";
public static $event="event";
public static $music="music";
public static $news="news";
}
class WX_EVENT_TYPE{
public static $subscribe="subscribe";
public static $unsubscribe="unsubscribe";
public static $scan="scan";
public static $location="LOCATION";
public static $click="CLICK";
}
class wechatCallbackApi
{
/**
* 当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。
* 大部分post用application/x-www.form-urlencoded标准的数据类型,可使用_POST接收。
* 但若是使用text/xml或者soap或者application/octet-stream;charset=UTF-8的数据类型就要用$GLOBALS["HTTP_RAW_POST_DATA"]来接收。
*
* post xml format
* <xml>
* <ToUserName><![CDATA[toUser]]></ToUserName>
* <FromUserName><![CDATA[fromUser]]></FromUserName>
* <CreateTime>1348831860</CreateTime>
* <MsgType><![CDATA[text]]></MsgType>
* <Content><![CDATA[this is a test]]></Content>
* <MsgId>1234567890123456</MsgId>
* </xml>
*
* respond xml format
* <xml>
* <ToUserName><![CDATA[toUser]]></ToUserName>
* <FromUserName><![CDATA[fromUser]]></FromUserName>
* <CreateTime>12345678</CreateTime>
* <MsgType><![CDATA[text]]></MsgType>
* <Content><![CDATA[你好]]></Content>
* </xml>
*/
public function respondMsg(){
$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];
if(!empty($postStr)){
$postXml= simplexml_load_string($postStr);
$toUserName=$postXml->ToUserName;
$fromUserName=$postXml->FromUserName;
$msgType=$postXml->MsgType;
$keyword=$postXml->Content;
$time=time();
$respondXmlTpl="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
if(!empty($keyword)){
$content=$keyword;
$respondXml=sprintf($respondXmlTpl,$fromUserName,$toUserName,$time,$msgType,$content);
echo $respondXml;
}else{
echo "input something...";
}
}else{
echo "nothing received.";
exit;
}
}
/**
* @return void
* @desc 开发者通过检验signature对请求进行校验(下面有校验方式)。
* 若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。
*/
public function valid(){
$echoStr=$_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
/**
* @desc 检验signature,加密/校验流程如下:
* 1. 将token、timestamp、nonce三个参数进行字典序排序
* 2. 将三个参数字符串拼接成一个字符串进行sha1加密
* 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
*
* @return bool
*/
private function checkSignature()
{
//GET /weixin.php?signature=b2cab3391bfff174e1ee46fca14d6b1a45aed9da&echostr=5948773415674059893×tamp=1384782965&nonce=1385056738
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
/* $signature = "b2cab3391bfff174e1ee46fca14d6b1a45aed9da";
$timestamp = "1384782965";
$nonce = "1385056738";
echo "signature=".$signature.'<hr>';
echo "timestamp=".$timestamp.'<hr>';
echo "nonce=".$nonce.'<hr>';
*/
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
//echo "implode tmpStr=".$tmpStr.'<hr>';
$tmpStr = sha1( $tmpStr );
//echo "sha1 tmpStr=".$tmpStr.'<hr>';
//echo "tmpStr == signature ? ".( $tmpStr == $signature ).'<hr>';
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
class Operator{
/**
*
* @param SimpleXMLElement $xmlMsg ,an object returned by "simplexml_load_string()"
*/
public static function operate($xmlMsg){
$msgType=$xmlMsg->MsgType;
switch ($msgType) {
case WX_MSG_TYPE::$text :
echo $msgType . PHP_EOL;
break;
case WX_MSG_TYPE::$image :
echo $msgType . PHP_EOL;
break;
case WX_MSG_TYPE::$voice :
echo $msgType . PHP_EOL;
break;
case WX_MSG_TYPE::$video :
echo $msgType . PHP_EOL;
break;
case WX_MSG_TYPE::$location :
echo $msgType . PHP_EOL;
break;
case WX_MSG_TYPE::$news :
echo $msgType . PHP_EOL;
break;
case WX_MSG_TYPE::$music :
echo $msgType . PHP_EOL;
break;
case WX_MSG_TYPE::$link :
echo $msgType . PHP_EOL;
break;
case WX_MSG_TYPE::$event:
echo $msgType.PHP_EOL;
$eventType=$xmlMsg->Event;
switch ($eventType){
case WX_EVENT_TYPE::$subscribe:
echo $eventType.PHP_EOL;
break;
case WX_EVENT_TYPE::$unsubscribe:
echo $eventType.PHP_EOL;
break;
case WX_EVENT_TYPE::$scan:
echo $eventType.PHP_EOL;
break;
case WX_EVENT_TYPE::$location:
echo $eventType.PHP_EOL;
break;
case WX_EVENT_TYPE::$click:
echo $eventType.PHP_EOL;
break;
default: echo "default handler for [".$eventType."] event ".PHP_EOL;
}
break;
default: echo "default handler for [".$msgType."] message ".PHP_EOL;
}
}
}
class weChatMessage{
public static $TYPE=array(
"text"=>"text",
"image"=>"image",
"voice"=>"voice",
"video"=>"video",
"location"=>"location",
"link"=>"link",
"event"=>"event",
"music"=>"music",
"news"=>"news"
);
public static $EVENT=array(
"subscribe"=>"subscribe",
"unsubscribe"=>"unsubscribe",
"scan"=>"scan",
"location"=>"LOCATION",
"click"=>"CLICK",
);
/**开发者微信号*/
public $toUserName;
/**发送方帐号(一个OpenID)*/
public $fromUserName;
/**消息创建时间 (整型)*/
public $createTime;
/**消息类型*/
public $msgType;
/**消息id,64位整型*/
public $msgId;
protected $xmlSpecial="<Content><![CDATA[This is a Text Message.]]></Content>";
protected $xmlSpecialRespond="<Content><![CDATA[This is a Text Message.]]></Content>";
protected $xmlSpecialRespondJson='{"content":"Hello World"}';
protected $xmlCommon=
"<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<MsgId>%s</MsgId>";
protected $xmlCommonRspond=
"<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>";
protected $xmlCommonRspondJson= '"touser":"%1$s", "msgtype":"%2$s","%2$s":';
function __construct(){
$this->toUserName="toUser接收人";
$this->fromUserName="fromUser发送人";
$this->msgType=WX_MSG_TYPE::$text;
$this->createTime=time();
}
protected function specialItemsToXml(){
return $this->xmlSpecial;
}
protected function commonItemsToXml(){
return sprintf($this->xmlCommon,$this->toUserName,$this->fromUserName,$this->createTime,$this->msgType,$this->msgId);
}
protected function specialItemsRespondXml(){
return $this->xmlSpecialRespond;
}
protected function specialItemsRespondJson(){
return $this->xmlSpecialRespondJson;
}
protected function commonItemsRespondXml(){ //$this->iterate();
return sprintf($this->xmlCommonRspond,$this->toUserName,$this->fromUserName,$this->createTime,$this->msgType);
}
protected function commonItemsRespondJson(){
return sprintf($this->xmlCommonRspondJson,$this->toUserName,$this->msgType);
}
protected function wrapWithMediaType($mediaType,$content){
return sprintf("<%s>%s</%s>",$mediaType,$content,$mediaType);
}
public function iterate(){
print static ::who()."::iterateVisible:\n";
foreach ($this as $key=>$value){
print "$key => $value\n";
}
}
private static function who(){
return get_called_class();
}
/**
* Received Message in xml format
**/
public function toXml(){
return "<xml>".$this->commonItemsToXml().$this->specialItemsToXml()."</xml>";
}
/**
* Respond Message in xml format
**/
public function respondXml(){
return "<xml>".$this->commonItemsRespondXml().$this->specialItemsRespondXml()."</xml>";
}
/**
* Respond Message in json format
**/
public function respondJson(){
return "{".$this->commonItemsRespondJson().$this->specialItemsRespondJson()."}";
}
}
class weChatTextMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$text;
}
/**文本消息内容*/
public $content;
protected $xmlSpecial="<Content><![CDATA[%s]]></Content>";
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->content);
}
}
class weChatImageMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$image;
}
/**图片链接*/
public $picUrl;
/**图片消息媒体id,可以调用多媒体文件下载接口拉取数据。*/
public $mediaId;
protected $xmlSpecial="<PicUrl><![CDATA[%s]]></PicUrl>
<MediaId><![CDATA[%s]]></MediaId>";
protected $xmlSpecialRespond="<MediaId><![CDATA[%s]]></MediaId>";
protected $xmlSpecialRespondJson='{"media_id":"%s"}';
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->picUrl,$this->mediaId);
}
protected function specialItemsRespondXml(){
$xml= sprintf($this->xmlSpecialRespond,$this->mediaId);
return $this->wrapWithMediaType("Image",$xml);
}
protected function specialItemsRespondJson(){
return sprintf($this->xmlSpecialRespondJson,$this->mediaId);
}
}
class weChatVoiceMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$voice;
}
/**语音格式,如amr,speex等*/
public $format;
/**语音消息媒体id,可以调用多媒体文件下载接口拉取数据。*/
public $mediaId;
protected $xmlSpecial="<MediaId><![CDATA[%s]]></MediaId>
<Format><![CDATA[%s]]></Format>";
protected $xmlSpecialRespond="<MediaId><![CDATA[%s]]></MediaId>";
protected $xmlSpecialRespondJson='{"media_id":"%s"}';
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->mediaId,$this->format);
}
protected function specialItemsRespondXml(){
$xml= sprintf($this->xmlSpecialRespond,$this->mediaId);
return $this->wrapWithMediaType("Voice",$xml);
}
protected function specialItemsRespondJson(){
return sprintf($this->xmlSpecialRespondJson,$this->mediaId);
}
}
/**
* 开通语音识别功能,用户每次发送语音给公众号时,微信会在推送的语音消息XML数据包中,增加一个Recongnition字段。
* 注:由于客户端缓存,开发者开启或者关闭语音识别功能,对新关注者立刻生效,对已关注用户需要24小时生效。
* 开发者可以重新关注此帐号进行测试。
*/
class weChatVoiceRecognitionMessage extends weChatVoiceMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$voice;
}
/**语音识别结果,UTF8编码*/
public $recognition;
protected $xmlSpecialExtend="<Recognition><![CDATA[%s]]></Recognition>";
protected function specialItemsToXml(){
$voiceSpecial=parent::specialItemsToXml();
return $voiceSpecial.sprintf($this->xmlSpecialExtend,$this->recognition);
}
}
class weChatVideoMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$video;
}
/**视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。*/
public $thumbMediaId;
/**视频消息媒体id,可以调用多媒体文件下载接口拉取数据。*/
public $mediaId;
protected $xmlSpecial="<MediaId><![CDATA[%s]]></MediaId>
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>";
protected $xmlSpecialRespondJson='{"media_id":"%s", "thumb_media_id":"%s"}';
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->mediaId,$this->thumbMediaId);
}
protected function specialItemsRespondXml(){
$xml= sprintf($this->xmlSpecial,$this->mediaId,$this->thumbMediaId);
return $this->wrapWithMediaType("Video",$xml);
}
protected function specialItemsRespondJson(){
return sprintf($this->xmlSpecialRespondJson,$this->mediaId,$this->thumbMediaId);
}
}
class weChatLocationMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$location;
}
/**地理位置纬度。*/
public $location_X;
/**地理位置经度。*/
public $location_Y;
/**地图缩放大小。*/
public $scale;
/**地理位置信息。*/
public $label;
protected $xmlSpecial="<Location_X>%s</Location_X>
<Location_Y>%s</Location_Y>
<Scale>%s</Scale>
<Label><![CDATA[%s]]></Label>";
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->location_X,$this->location_Y,$this->scale,$this->label);
}
}
class weChatLinkMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$link;
}
/**消息标题*/
public $title;
/**消息描述*/
public $description;
/**消息链接*/
public $url;
protected $xmlSpecial="<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<Url><![CDATA[%s]]></Url>";
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->title,$this->description,$this->url);
}
}
class weChatMusicMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$music;
}
/**音乐标题*/
public $title;
/**音乐描述*/
public $description;
/**音乐链接*/
public $music_url;
/**高品质音乐链接*/
public $hq_music_url;
/**音乐消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。*/
public $thumbMediaId;
protected $xmlSpecial="<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>";
protected $xmlSpecialRespondJson='{ "title":"%s",
"description":"%s",
"musicurl":"%s",
"hqmusicurl":"%s",
"thumb_media_id":"%s" }';
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->title,$this->description,$this->music_url,$this->hq_music_url,$this->thumbMediaId);
}
protected function specialItemsRespondXml(){
$xml= $this->specialItemsToXml();
return $this->wrapWithMediaType("Music",$xml);
}
protected function specialItemsRespondJson(){
return sprintf($this->xmlSpecialRespondJson,$this->title,$this->description,$this->music_url,$this->hq_music_url,$this->thumbMediaId);
}
}
class weChatNewsMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$news;
}
/**
* 多条图文消息信息,默认第一个item为大图,限制为10条以内
* 是一个article数组,每个article包含以下四项:
* title:图文消息标题
* description:图文消息描述
* picUrl:图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
* url:点击图文消息跳转链接
*/
public $articles;
protected $xmlSpecial="<ArticleCount>%s</ArticleCount>
<Articles>%s</Articles>";
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,count($this->articles),$this->dumpArticlesXml($this->articles));
}
protected function specialItemsRespondXml(){
return $this->specialItemsToXml();
}
protected function specialItemsRespondJson(){
return sprintf('"{articles": [%s]}',$this->dumpArticlesJson($this->articles));
}
private function dumpArticlesXml(&$articles){
$xml="";
foreach($articles as &$article){
$xml.= $this->articleXml($article);
}
return $xml;
}
private function articleXml($article){
$tpl="<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
return sprintf($tpl,$article["title"],$article["description"],$article["picUrl"],$article["url"]);
}
private function dumpArticlesJson(&$articles){
$json="";
foreach($articles as &$article){
$json.= $this->articleJson($article).',';
}
return rtrim($json,',');
}
private function articleJson($article){
$tpl='{
"title":"%s",
"description":"%s",
"picurl":"%s",
"url":"%s"
}';
return sprintf($tpl,$article["title"],$article["description"],$article["picUrl"],$article["url"]);
}
}
/**
* 关注/取消关注事件
* 用户在关注与取消关注公众号事,微信会把这个事件推送到开发者填写的URL。
* 方便开发者给用户下发欢迎消息或者做帐号的解绑。
*/
class weChatEventMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$event;
$this->event=WX_EVENT_TYPE::$subscribe;
}
/**事件类型,关注/取消关注事件,subscribe(订阅)、unsubscribe(取消订阅)*/
public $event;
protected $xmlSpecial="<Event><![CDATA[%s]]></Event>";
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->event);
}
}
/**
* 自定义菜单事件
* 用户点击自定义菜单后,如果菜单按钮设置为click类型,则微信会把此次点击事件推送给开发者,
* 注意view类型(跳转到URL)的菜单点击不会上报。
*/
class weChatMenuEventMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$event;
$this->event=WX_EVENT_TYPE::$click;
}
/**事件类型,CLICK*/
public $event;
/**事件KEY值,与自定义菜单接口中KEY值对应*/
public $eventKey;
protected $xmlSpecial="<Event><![CDATA[%s]]></Event><EventKey><![CDATA[%s]]></EventKey>";
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->event,$this->eventKey);
}
}
/**
* 扫描二维码事件
* 用户扫描带场景值二维码时,可能推送以下两种事件:
* 如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。
* 如果用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者。
*/
class weChatQREventMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$event;
$this->event=WX_EVENT_TYPE::$scan;
}
/**事件类型,未关注时subscribe(订阅)、已关注时scan*/
public $event;
/**事件KEY值,
* 未关注时是"qrscene_123123",qrscene_为前缀,后面为二维码的参数值。
* 已关注时是一个32位无符号整数*/
public $eventKey;
/**二维码的ticket,可用来换取二维码图片*/
public $ticket;
protected $xmlSpecial="<Event><![CDATA[%s]]></Event>
<EventKey><![CDATA[%s]]></EventKey>
<Ticket><![CDATA[%s]]></Ticket>";
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->event,$this->eventKey,$this->ticket);
}
}
/**用户同意上报地理位置后,每次进入公众号会话时,都会在进入时上报地理位置,
* 或在进入会话后每5秒上报一次地理位置,公众号可以在公众平台网站中修改以上设置。
* 上报地理位置时,微信会将上报地理位置事件推送到开发者填写的URL。
*/
class weChatLocationEventMessage extends weChatMessage {
function __construct(){
parent::__construct();
$this->msgType=WX_MSG_TYPE::$event;
$this->event=WX_EVENT_TYPE::$location;
}
/**事件类型,LOCATION*/
public $event;
/**地理位置纬度*/
public $latitude;
/**地理位置经度*/
public $longitude;
/**地理位置精度*/
public $precision;
protected $xmlSpecial="<Event><![CDATA[%s]]></Event>
<Latitude>%s</Latitude>
<Longitude>%s</Longitude>
<Precision>%s</Precision>";
protected function specialItemsToXml(){
return sprintf($this->xmlSpecial,$this->event,$this->latitude,$this->longitude,$this->precision);
}
}
function testMessages(){
/*
$msg=new weChatMessage();
$msg->toUserName="toUser接收人";
$msg->fromUserName="fromUser发送人";
$msg->msgType="text";
$msg->createTime=time();
echo $msg->toXml().PHP_EOL;
echo $msg->respondXml().PHP_EOL;
$textMsg=new weChatTextMessage();
echo $textMsg->toXml().PHP_EOL;
$imageMsg=new weChatImageMessage();
echo $imageMsg->toXml().PHP_EOL;
echo $imageMsg->respondXml().PHP_EOL;
$voiceMsg=new weChatVoiceMessage();
echo $voiceMsg->toXml().PHP_EOL;
$voiceRecognitionMsg=new weChatVoiceRecognitionMessage();
echo $voiceRecognitionMsg->toXml().PHP_EOL;
$videoMsg=new weChatVideoMessage();
echo $videoMsg->toXml().PHP_EOL;
$locationMsg=new weChatLocationMessage();
echo $locationMsg->toXml().PHP_EOL;
$linkMsg=new weChatTextMessage();
echo $linkMsg->toXml().PHP_EOL;
*/
//Change the Messag type to test all kind of messages.
$msg=new weChatNewsMessage();
function setParams($msg){
$articles=array(
array("title"=>"标题1","description"=>"描述1","picUrl"=>"http://www.imdevice.com/10000/pic.jpg","url"=>"http://www.imdevice.com/10000"),
array("title"=>"标题2","description"=>"描述2","picUrl"=>"http://www.imdevice.com/20000/pic.jpg","url"=>"http://www.imdevice.com/20000")
);
$msg->content="消息内容";
$msg->eventKey="关键字";
$msg->msgId="1213871409182349";
$msg->ticket="二维码897140987123987410";
$msg->latitude="23.137466";
$msg->longitude="113.352425";
$msg->precision="119.385040";
$msg->location_X="23.137466";
$msg->location_Y="113.352425";
$msg->label="地理位置信息";
$msg->picUrl="abc.com/pic.jpg";
$msg->mediaId="MEDIA_ID";
$msg->thumbMediaId="THUMB_MEDIA_ID";
$msg->scale="20";
$msg->format="如amr,speex等";
$msg->recognition="语音识别结果,UTF8编码";
$msg->articles=$articles;
$msg->title="Title";
$msg->description="description";
$msg->url="http://imdevice.com";
$msg->music_url="http://imdevice.com/song.mp3";
$msg->hq_music_url="http://imdevice.com/hqsong.mp3";
//$msg->event=WX_EVENT_TYPE::$unsubscribe;
}
setParams($msg);
//$xmlMsg=simplexml_load_string($msg->toXml());
//echo $xmlMsg->count().PHP_EOL;
//var_dump(get_object_vars($xmlMsg));
//var_dump(property_exists($xmlMsg, "Content"));
echo $msg->respondJson();
$xmlMsg=simplexml_load_string($msg->respondXml());
Operator::operate($xmlMsg);
echo $xmlMsg->asXML();
}
//testMessages();
function responseMsg(){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$xmlMsg=simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
Operator::operate($xmlMsg);
}else{
echo "";
exit;
}
}
responseMsg();
?>