微信开发学习(二)

本文介绍了微信接收的不同类型消息的XML格式,包括文本、图片、语音、视频、地理位置及链接消息,并提供了PHP代码示例用于解析这些消息。

学习发送微信的消息类型。

当发送消息时,同时会附加的参数有,signature,timestamp,nonce, 以及消息的XML数据包。

数据包格式,类似:

<xml>

  <ToUserName><![CDATA[tousername]]></ToUserName>

    <FromUserName><![CDATA[fromusername]]></FromUserName>

    <CreateTime><!CDATA[createtime]]</CreateTime>

  <MsgType><!CDATA[type]]</MsgType>

<Content><!CDATA[content]]</Content>

<MsgId>xxxxxx</MsgId>

</xml>

 

收到原始数据的代码:

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

将原始数据转变为对象:

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

 

1. 文本消息

    

<xml>

  <ToUserName><![CDATA[tousername]]></ToUserName>

    <FromUserName><![CDATA[fromusername]]></FromUserName>

    <CreateTime><!CDATA[createtime]]</CreateTime>

  <MsgType><!CDATA[text]]</MsgType>

<Content><!CDATA[content]]</Content>

<MsgId>xxxxxx</MsgId>

</xml>

 

2. 图片消息

<xml>

  <ToUserName><![CDATA[tousername]]></ToUserName>

    <FromUserName><![CDATA[fromusername]]></FromUserName>

    <CreateTime><!CDATA[createtime]]</CreateTime>

  <MsgType><!CDATA[image]]</MsgType>

<PicUrl><!CDATA[http://xxxxxx]]</PicUrl>

<MsgId>xxxxxx</MsgId>

<MediaId><![CDATA[xxxxxx]]></MediaId>

</xml>

3. 语音消息

<xml>

  <ToUserName><![CDATA[tousername]]></ToUserName>

    <FromUserName><![CDATA[fromusername]]></FromUserName>

    <CreateTime><!CDATA[createtime]]</CreateTime>

  <MsgType><!CDATA[voice]]</MsgType>

 <MediaId><![CDATA[xxxxxx]]></MediaId>

 <Format><![CDATA[amr]]></Format>

<MsgId>xxxxxx</MsgId>

<Recognition><![CDATA[]]></Recognition>

</xml>

 

4.视频消息

<xml>

  <ToUserName><![CDATA[tousername]]></ToUserName>

    <FromUserName><![CDATA[fromusername]]></FromUserName>

    <CreateTime><!CDATA[createtime]]</CreateTime>

  <MsgType><!CDATA[video]]</MsgType>

 <MediaId><![CDATA[xxxxxx]]></MediaId>

<ThumbMediaId><![CDATA[xxxx]]></ThumbMediaId>

<MsgId>xxxxxx</MsgId>

</xml>

 

5.地理位置消息

<xml>

  <ToUserName><![CDATA[tousername]]></ToUserName>

    <FromUserName><![CDATA[fromusername]]></FromUserName>

    <CreateTime><!CDATA[createtime]]</CreateTime>

   <MsgType><!CDATA[location]]</MsgType>

 <Location_X>xxxxx</Location_X>

<Location_Y>xxxx<Location_Y>

<Scale>xx</Scale>

<Label><![CDATA[xxxxxx]]></Label>

<MsgId>xxxxxx</MsgId>

</xml>

 

6.连接消息

<xml>

  <ToUserName><![CDATA[tousername]]></ToUserName>

    <FromUserName><![CDATA[fromusername]]></FromUserName>

    <CreateTime><!CDATA[createtime]]</CreateTime>

  <MsgType><!CDATA[link]]</MsgType>

 <Title><![CDATA[xxxxxx]]></Title>

<Description><![CDATA[xxxx]]></Description>

<Url><![CDATA[xxxx]]></Url>

<MsgId>xxxxxx</MsgId>

</xml>

转载于:https://www.cnblogs.com/ahMay/p/5151273.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值