微信公众平台开发 在线点歌 音乐接口
琼中中立科技 | 2018-01-26 23:47:30 | 阅读:39190
一、获取音乐资源方倍工作室推出音乐API接口,目前处于测试阶段,欢迎各位试用。调用方法:如调用“最炫民族风”,传递其其URL编码%e6%9c%80%e7%82%ab%e6%b0%91%e6%97%8f%e9%a3%8e就可以了。以下url可以直接在IE8中打开,能自动转码。http://api2.sinaapp.com/search/music/?appkey=0020130430&appsecert=fa6095e1133d28ad&reqtype=music&keyword=%e6%9c%80%e7%82%ab%e6%b0%91%e6%97%8f%e9%a3%8e复制代码返回格式:与微信音乐格式类似,json型:{"errcode":0,"msgtype":"music","music":{"title":"最炫民族风","description":"music","musicurl":"http://stream10.qqmusic.qq.com/31432174.mp3","hqmusicurl":"http://stream10.qqmusic.qq.com/31432174.mp3"}}复制代码失败时返回文本信息{"errcode":"1","msgtype":"text","text":{"content":"检索音乐失败!"}}复制代码二、封装音乐消息获取该音乐消息的代码如下:elseif(substr($keyword,0,6)=="点歌"){$entityName=trim(substr($keyword,6,strlen($keyword)));if($entityName==""){$contentStr="发送“点歌”加上歌名,如“点歌最炫民族风”";$resultStr=$this->transmitText($object,$contentStr,$funcFlag);return$resultStr;}$apihost="http://api2.sinaapp.com/";$apimethod="search/music/?";$apiparams=array('appkey'=>"0020120430",'appsecert'=>"fa6095e113cd28fd",'reqtype'=>"music");$apikeyword="&keyword=".urlencode($entityName);$apicallurl=$apihost.$apimethod.http_build_query($apiparams).$apikeyword;$api2str=file_get_contents($apicallurl);$api2json=json_decode($api2str,true);$musicUrl=$api2json['music']['hqmusicurl'];if($musicUrl==""){$contentStr="没有找到音乐,可能不是歌名或者检索失败,请换首歌试试!";$resultStr=$this->transmitText($object,$contentStr,$funcFlag);}else{$musicArray=array("title"=>$api2json['music']['title'],"description"=>$api2json['music']['description'],"MusicUrl"=>$api2json['music']['musicurl'],"HQMusicUrl"=>$api2json['music']['hqmusicurl']);$resultStr=$this->transmitMusic($object,$musicArray,$funcFlag);}return$resultStr;}复制代码三、与微信对接参数描述ToUserName接收方帐号(收到的OpenID)FromUserName开发者微信号CreateTime消息创建时间MsgTypemusicMusicUrl音乐链接HQMusicUrl高质量音乐链接,WIFI环境优先使用该链接播放音乐FuncFlag位0x0001被标志时,星标刚收到的消息。四、效果演示需要添加天气神,然后发送m,可以看到相关使用说明1.发送“点歌”加歌名,如“点歌最炫民族风”,返回指定的歌曲。2.只发送“点歌”两个字,系统为您选择一首热门歌曲。3.按住说话按钮2秒钟再松开,随机返回一首热门歌曲。歌曲在WIFI环境中播放很流畅,有很好地体验。