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

微信公众平台API测试——接收图片消息

2020-03-29 18:33 1041 查看

一、简介

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

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

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

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

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

二、适用场景

    普通微信用户通过微信给公众账号发送的图片时,微信服务器图片上传到微信的服务器并将图片的URL封装为XML消息格式发送到开发者URL。服务器上可以通过消息中带的图片URL和MediaId调用多媒体文件下载接口拉取数据。

 

三、消息格式说明

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<PicUrl><![CDATA[this is a url]]></PicUrl>
<MediaId><![CDATA[media_id]]></MediaId>
<MsgId>1234567890123456</MsgId>
</xml>

 

参数描述
ToUserName 开发者微信号
FromUserName 发送方帐号(一个OpenID)
CreateTime 消息创建时间 (整型)
MsgType image
PicUrl 图片链接
MediaId 图片消息媒体id,可以调用多媒体文件下载接口拉取数据。
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                     default:
30                         $resultStr = "Unknow message type: " . $MSG_TYPE;
31                         break;
32                 }
33                 //echo $postStr;
34                 echo $resultStr;
35         }else {
36             echo "";
37             exit;
38         }
39     }
40
41     private function handleImage($postObj)
42     {
43         //获取图片相关信息进行处理
44         $picURL = trim($postObj->PicUrl);
45         //$mediaID = trim($postObj->MediaId);
46
47         if(!empty($picURL)){
48             $contentStr = "URL : " . $picURL;
49             $resultStr = $this->responseText($postObj, $contentStr);
50         }else{
51             $resultStr = "Picture URL is empty.";
52         }
53
54         return $resultStr;
55     }
56
57
58     private function responseText($object, $content, $flag=0)
59     {
60         $textTpl = "<xml>
61                     <ToUserName><![CDATA[%s]]></ToUserName>
62                     <FromUserName><![CDATA[%s]]></FromUserName>
63                     <CreateTime>%s</CreateTime>
64                     <MsgType><![CDATA[text]]></MsgType>
65                     <Content><![CDATA[%s]]></Content>
66                     <FuncFlag>%d</FuncFlag>
67                     </xml>";
68         $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
69         return $resultStr;
70     }
71
72 }
73
74 ?>

 

 

疑问:

通过微信平台接口调试工具,发现下方的消息中,没有<MediaId><![CDATA[media_id]]></MediaId>字段  

<xml>
<URL>
<![CDATA[http://********.my.phpcloud.com/weixinapi/wx_handleMsg.php]]>
</URL>
<ToUserName>
<![CDATA[ToUser]]>
</ToUserName>
<FromUserName>
<![CDATA[FromUser]]>
</FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType>
<![CDATA[image]]>
</MsgType>
<PicUrl>
<![CDATA[http://e.hiphotos.baidu.com/image/w%3D2048/sign=17735c757af0f736d8fe4b013e6db319/1e30e924b899a901f01f313c1f950a7b0208f596.jpg]]>
</PicUrl>
<MsgId>1234567890123456</MsgId>
</xml>

 

   

 

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

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