微信开发教程:[5]音乐消息的回复
1、接口文件(wx_sample)全部代码如下:争犸禀淫<?phpinclude_once("wx_tpl.php");//装载数据格式模板文件$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//获取微信发送数据 //返回回复数据if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);//解析数据 $fromUsername = $postObj->FromUserName;//发送消息方Id $toUsername = $postObj->ToUserName;//接收消息方ID $form_MsgType = $postObj->MsgType;//消息类型 //事件消息 if($form_MsgType=="event"){ $form_Event = $postObj->Event;//获取事件类型 //订阅事件 if($form_Event=="subscribe") { $msgType = "music"; $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType, "歌名", "歌手","http://weixincourse-weixincourse.stor.sinaapp.com/mysongs.aac", "http://weixincourse-weixincourse.stor.sinaapp.com/mysongs.mp3"); echo $resultStr; exit;} } } else { echo ""; exit;}?>
![微信开发教程:[5]音乐消息的回复](https://exp-picture.cdn.bcebos.com/def72c6c576699cffa0891d0a885e036e3915ef3.jpg)
3、实现效果。将上面两个文件放置到空间就可以实现效果(前提是接口已经配置好):当用户订阅公众号的时候,收到一首歌曲,如图:
![微信开发教程:[5]音乐消息的回复](https://exp-picture.cdn.bcebos.com/27725684cde34b2c43b2c5cc0d0e7c75e4f443f3.jpg)
5、代码讲解2:$msgType = "music";规定回复的消息时音乐。
![微信开发教程:[5]音乐消息的回复](https://exp-picture.cdn.bcebos.com/65390a23beb9763e079e05656ad06de89b61b0f3.jpg)