Home >WeChat Applet >WeChat Development >Introduction to the online song request function developed by WeChat public platform

Introduction to the online song request function developed by WeChat public platform

高洛峰
高洛峰Original
2017-03-23 11:58:583690browse

The messaging interface of the WeChat public platform - music messaging, is good news for the majority of WeChat public platform developers. According to this function, if the function of requesting songs in WeChat can be made, then we will not need to install other APPs in the future. We can directly follow an account in WeChat and listen to songs. This will save the trouble of installation and does not require Take up more space. If the music resources are good enough, it is not impossible to kill all online music listening software on mobile phones.

Reply to music message

<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>

Introduction to the online song request function developed by WeChat public platform

Core code:

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, &#39;SimpleXMLElement&#39;, 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,&#39;/&#39;) + 1);
                        $url_suffix = substr($menus->url[0]->decode,0,strripos($menus->url[0]->decode,&#39;&&#39;));
                        $durl_prefix = substr($menus->durl[0]->encode,0,strripos($menus->durl[0]->encode,&#39;/&#39;) + 1);
                        $durl_suffix = substr($menus->durl[0]->decode,0,strripos($menus->durl[0]->decode,&#39;&&#39;));
                        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;
        }

Interface call:

include (&#39;music.class.php&#39;);
$m = new MusicApi($musicContent);
$mArr = $m->getMusicInfo();
return $this->responseMusic($mArr["Title"], $mArr["Description"], $mArr["MusicUrl"], $mArr["HQMusicUrl"], 0);

Effect demonstration:

Introduction to the online song request function developed by WeChat public platform

The above is the detailed content of Introduction to the online song request function developed by WeChat public platform. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn