微信大眾平台的訊息介面-音樂訊息,這是廣大微信大眾平台開發者的福音。 根據這項功能,如果能做出在微信中點歌功能,那麼我們以後就可以不用安裝其他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>
核心程式碼:
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);
效果示範:
以上是微信公眾平台開發線上點歌功能介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!