您的位置:首页 > 移动开发 > 微信开发

微信公众平台API测试——接收地理位置消息

2020-03-29 18:33 876 查看

一、简介

     当普通微信用户向公众账号发消息时,微信服务器将用户发送的消息封装成XML数据包,通过POST消息发送到开发者的URL上。

     微信服务器在五秒内收不到服务器的响应会断掉连接,并且重新发起请求,总共重试三次。关于重试的消息排重,推荐使用msgid排重。

     假如服务器无法保证在五秒内处理并回复,可以直接回复空串,微信服务器不会对此作任何处理,并且不会发起重试。

当前支持如下的普通消息:

  • 1 文本消息
  • 2 图片消息
  • 3 语音消息
  • 4 视频消息
  • 5 地理位置消息
  • 6 链接消息

二、适用场景

    普通微信用户通过微信给公众账号发送的位置信息,微信服务器将收到的位置信息封装为位置消息发送到开发者URL。开发者可以从消息中解析出:经度,纬度,当前地图缩放比例,位置描述。通过上述信息,可以精确定位发送位置的用户。

三、消息格式说明

1 <xml>
2 <ToUserName><![CDATA[toUser]]></ToUserName>
3 <FromUserName><![CDATA[fromUser]]></FromUserName>
4 <CreateTime>1351776360</CreateTime>
5 <MsgType><![CDATA[location]]></MsgType>
6 <Location_X>23.134521</Location_X>
7 <Location_Y>113.358803</Location_Y>
8 <Scale>20</Scale>
9 <Label><![CDATA[位置信息]]></Label>
10 <MsgId>1234567890123456</MsgId>
11 </xml>
参数描述
ToUserName 开发者微信号
FromUserName 发送方帐号(一个OpenID)
CreateTime 消息创建时间 (整型)
MsgType location
Location_X 地理位置维度
Location_Y 地理位置经度
Scale 地图缩放大小
Label 地理位置信息
MsgId 消息id,64位整型

  四、代码示例

1 <?php
2 /**
3   * wechat php test
4   */
5
6 //define your token
7 define("TOKEN", "weixin");
8 $wechatObj = new wechatCallbackapiTest();
9 $wechatObj->responseMsg();
10
11 class wechatCallbackapiTest
12 {
13     public function responseMsg()
14     {
15         //get post data, May be due to the different environments
16         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
17
18         if (!empty($postStr)){
19                 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
20                 $MSG_TYPE = trim($postObj->MsgType);
21
22                 switch ($MSG_TYPE) {
23                     case "text":
24                         $resultStr = $this->handleText($postObj);
25                         break;
26                     case "image":
27                         $resultStr = $this->handleImage($postObj);
28                         break;
29                     case "voice":
30                         $resultStr = $this->handleVoice($postObj);
31                         break;
32                     case "video":
33                         $resultStr = $this->handleVideo($postObj);
34                         break;
35                      case "location":
36                         $resultStr = $this->handleLocation($postObj);
37                         break;
38                     default:
39                         $resultStr = "Unknow message type: " . $MSG_TYPE;
40                         break;
41                 }
42                 //echo $postStr;
43                 echo $resultStr;
44         }else {
45             echo "";
46             exit;
47         }
48     }
49
50     private function handleLocation($postObj)
51     {
52         //获取纬度
53         $latitude  = trim($postObj->Location_X);
54         //获取经度
55         $longitude = trim($postObj->Location_Y);
56
57         $scale = trim($postObj->Scale);
58
59         $locationLabel = trim($postObj->Label);64
65         if(!empty($latitude)){
66             $contentStr = "Latitude : " . $latitude ."\n"
67                         . "Longitude : " . $longitude . "\n"
68                         . "Scale : " . $scale . "\n"
69                          . "Label : " . $locationLabel . "\n" ;
70             $resultStr = $this->responseText($postObj, $contentStr);
71         }else{
72             $resultStr = "MediaId is empty.";
73         }
74
75         return $resultStr;
76     }
77
78     private function responseText($object, $content, $flag=0)
79     {
80         $textTpl = "<xml>
81                     <ToUserName><![CDATA[%s]]></ToUserName>
82                     <FromUserName><![CDATA[%s]]></FromUserName>
83                     <CreateTime>%s</CreateTime>
84                     <MsgType><![CDATA[text]]></MsgType>
85                     <Content><![CDATA[%s]]></Content>
86                     <FuncFlag>%d</FuncFlag>
87                     </xml>";
88         $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
89         return $resultStr;
90     }
91
92 }
93
94 ?>

五、获取到的结果

 

转载于:https://www.cnblogs.com/youlangu/p/3699901.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
amwhbngv30764 发布了0 篇原创文章 · 获赞 0 · 访问量 37 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐