[转]百度MP3音乐API接口及应用
当你在百度去搜索一首歌时,你会发现有种更简单的方法,嘿嘿,告诉你个秘密,百度有个不公开的API
http://box.zhangmen.baidu.com/x?op=12&count=1&title=一直想着他$$庄心妍$$$$
用上面的地址,红色部分改成歌名与作者名,然后百度就会给你一个XML:
<result> <count>1</count> <url> <encode> <![CDATA[ http://zhangmenshiting.baidu.com/data2/music/105312949/YmJoZ2ZocGxqYKCkaHWvm6CWmHFla25pYmhlmZhuaGlmlpiZa5iZmpNqlJhta2xtY5VsmmmamHCSZpdnbGhvmZJiZGpbo6CcbmJhZW1naG1pY2ZlaGpsbGky ]]> </encode> <decode> <![CDATA[ 105312949.mp3?xcode=0571162ec8115dee6bbbb8ad85552c9f4da8a4d3728aa016&mid=0.18115813134548 ]]> </decode> <type>8</type> <lrcid>0</lrcid> <flag>0</flag> </url> <durl> <encode> <![CDATA[ http://zhangmenshiting2.baidu.com/data2/music/105313027/YmJoZ2ZpZ2poYKCkaHWvm6CWmHFla25pYmhlmZhuaGlmlpiZa5iZmpNqlJhta2xtY5VsmmeXm55qaWVoamqZa2mWZmxbo6CcbmJhZW1naG1pY2ZlaGpsbGky ]]> </encode> <decode> <![CDATA[ 105313027.mp3?xcode=0571162ec8115dee6bbbb8ad85552c9f2adf972454b38d38&mid=0.18115813134548 ]]> </decode> <type>8</type> <lrcid>0</lrcid> <flag>0</flag> </durl> </result>
PHP解析:
public function getMusicContent($postObj) { $ret= "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> <Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> <FuncFlag><![CDATA[1]]></FuncFlag> </Music> </xml>"; $recognition=$postObj->Recognition; $keywordc= urlencode($recognition); $musicapi = "http://box.zhangmen.baidu.com/x?op=12&count=1&title={$keywordc}\$\$"; $simstr=file_get_contents($musicapi); $musicobj=simplexml_load_string($simstr); $i=0; $musicurl; foreach($musicobj->url as $itemobj) { $encode = $itemobj->encode; $decode = $itemobj->decode; $removedecode = end(explode('&', $decode)); if($removedecode<>"") { $removedecode="&".$removedecode; } $decode = str_replace($removedecode,"", $decode); $musicurl= str_replace(end(explode('/', $encode)) ,$decode,$encode); break; } $resultStr = sprintf($ret, $postObj->FromUserName, $postObj->ToUserName,time(), $recognition,$musicurl,$musicurl); return $resultStr; }