微信公众平台的消息接口-音乐消息,这是广大微信公众平台开发者的福音。 根据这一功能,如果能做出在微信中点歌功能,那么我们以后就可以不用安装其他APP, 直接在微信里面关注一个账号,就可以听歌了,还省去了安装的麻烦,而且不用再占用空间。如果音乐资源足够好,那将秒杀一切手机上在线听歌软件也不是没有可能的。
回复音乐消息
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[music]]></MsgType> <Music> <Title><![CDATA[TITLE]]></Title> <Description><![CDATA[DESCRIPTION]]></Description> <MusicUrl><![CDATA[MUSIC_Url]]></MusicUrl> <HQMusicUrl><![CDATA[HQ_MUSIC_Url]]></HQMusicUrl> <ThumbMediaId><![CDATA[media_id]]></ThumbMediaId> </Music> </xml>
参数 | 是否必须 | 说明 |
---|---|---|
ToUserName | 是 | 接收方帐号(收到的OpenID) |
FromUserName | 是 | 开发者微信号 |
CreateTime | 是 | 消息创建时间 (整型) |
MsgType | 是 | music |
Title | 否 | 音乐标题 |
Description | 否 | 音乐描述 |
MusicURL | 否 | 音乐链接 |
HQMusicUrl | 否 | 高质量音乐链接,WIFI环境优先使用该链接播放音乐 |
ThumbMediaId | 是 | 缩略图的媒体id,通过上传多媒体文件,得到的id |
核心代码:
public function getMusicInfo() {if ($this->name == ""){$content = array( "Title"=>"","Description"=>"你还没告诉我音乐名称呢?","MusicUrl"=>"","HQMusicUrl"=>"");} else {if (strpos($this->name, "+")){$music = explode("+",$this->name);$url = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=".$music[1]."$$".$music[0]."$$$$";}else{$url = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=".$this->name."$$";}$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);$data = curl_exec($ch);$content = array( "Title"=>"歌曲【".$this->name."】","Description"=>"检索失败","MusicUrl"=>"","HQMusicUrl"=>"");try{@$menus = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);if ($menus->count > 0 && isset($menus->url[0]) && isset($menus->durl[0])){$url_prefix = substr($menus->url[0]->encode,0,strripos($menus->url[0]->encode,'/') + 1);$url_suffix = substr($menus->url[0]->decode,0,strripos($menus->url[0]->decode,'&'));$durl_prefix = substr($menus->durl[0]->encode,0,strripos($menus->durl[0]->encode,'/') + 1);$durl_suffix = substr($menus->durl[0]->decode,0,strripos($menus->durl[0]->decode,'&'));if (strpos($this->name, "+")){$content = array( "Title"=>$music[1],"Description"=>$music[0],"MusicUrl"=>$url_prefix.$url_suffix,"HQMusicUrl"=>$durl_prefix.$durl_suffix);}else{$content = array( "Title"=>$this->name,"Description"=>"百度音乐提供","MusicUrl"=>$url_prefix.$url_suffix,"HQMusicUrl"=>$durl_prefix.$durl_suffix);}}}catch(Exception $e){}}return $content;}
接口调用:
include ('music.class.php');
$m = new MusicApi($musicContent);
$mArr = $m->getMusicInfo();
return $this->responseMusic($mArr["Title"], $mArr["Description"], $mArr["MusicUrl"], $mArr["HQMusicUrl"], 0);
效果演示:
欢迎关注微信公众号:音比特 微信号:yinbyte 或者用微信扫描下面二维码来关注
转载于:https://blog.51cto.com/azhome/1562962