前段时间喜欢上了一个音乐播放器,为了寻找音乐外链苦恼好久,后来无意间发现酷狗Web音乐播放器有一个使用jsonp查找音乐的接口,后来网上查了一下,发现很多个地方都有酷狗音乐可用的接口,于是自己挖出来几个可用的接口写了一个类,目前音乐播放器用的就是这个查找的歌曲.
来源
当模拟手机浏览器访问酷狗音乐的时候,酷狗的歌曲搜索接口是一个可用的json或者jsonp接口,如图:
我们再看看接口返回的数据
1 |
kgJSONP344476656({"status":1,"error":"","data":{"timestamp":1451023834,"correctiontype":0,"info":[{"filename":"音美 - 小幸运","extname":"mp3","m4afilesize":1154488,"320hash":"","mvhash":"e9ccb681d6628a45a73e2627ae7bcadc","privilege":0,"filesize":4496213,"source":"sc","group":[],"ownercount":332390,"topic":"","320filesize":0,"isnew":1,"duration":281,"album_name":"","sqhash":"","singername":"音美","album_id":"","320privilege":0,"sourceid":1,"sqfilesize":0,"srctype":1,"Accompany":0,"hash":"3aaa3126ee0be0a9920783510a1691eb","sqprivilege":0,"bitrate":128,"feetype":0},{"filename":"燃烧fancy - 小幸运","extname":"mp3","m4afilesize":1109729,"320hash":"","mvhash":"","privilege":0,"filesize":4329265,"source":"sc","group":[],"ownercount":115,"topic":"","320filesize":0,"isnew":0,"duration":271,"album_name":"","sqhash":"","singername":"燃烧fancy","album_id":"","320privilege":0,"sourceid":1,"sqfilesize":0,"srctype":1,"Accompany":0,"hash":"057489bf2ac0b900132bf3b860a05ba1","sqprivilege":0,"bitrate":128,"feetype":0}],"total":78,"istag":0,"correctiontip":"","forcecorrection":0,"istagresult":0},"errcode":0}) |
数据比较多,我删掉了大部分,
那我们再看看请求参数,好好分析一下:
http://mobilecdn.kugou.com/api/v3/search/song?format=jsonp&keyword=%E5%B0%8F%E5%B9%B8%E8%BF%90&page=1&pagesize=30&showtype=1&callback=kgJSONP344476656
没错,这就是刚才的请求地址,
大概可以猜出来的是:
format=jsonp 是返回格式
keyword=%E5%B0%8F%E5%B9%B8%E8%BF%90 这是关键词
page=1 页数
pagesize=30 条数
showtype=1 暂未知
callback=kgJSONP344476656 jsonp的callback
经过测试,事实证明是对的,
于是就有了下面这个类,当然歌词搜索功能是其它接口….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php namespace Library\Music; class Kugou { /** * [kgMusicSearch 根据关键词查找歌曲] * @param [type] $keyword [description] * @param string $type [description] * @param string $total [description] * @return [type] [description] */ public function kgMusicSearch($keyword, $total = '30', $type = 'json') { $keyword = isset($keyword) ? $keyword : '金莎'; /*http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=想象之中&page=1&pagesize=30*/ $url = 'http://mobilecdn.kugou.com/api/v3/search/song?format=' . $type . '&keyword=' . $keyword . '&page=1&pagesize=' . $total; $MusicList = https_request($url); $MusicList = json_decode($MusicList, true); if(empty($MusicList['error'])) { return $MusicList['data']['info']; } else { ajaxReturn(array('code' => -1, 'message' => '歌曲未搜到')); } } /** * [kgMusicFind 酷狗音乐根据Hush查找歌曲详细信息] * @param [type] $hush [hush] * @return [type] [description] */ public function kgMusicFind($hash) { $hash = substr($hash, strpos($hash, '_') + 1, 1000); $url = 'http://m.kugou.com/app/i/getSongInfo.php?hash=' . $hash . '&cmd=playInfo'; $Music = https_request($url); $Music = json_decode($Music, true); $auth = substr($Music['fileName'], 0, strpos($Music['fileName'], '-')); //酷狗歌曲格式 金莎 - 星月神话 $MusicGb = $this->GetAuthImg(trim($auth)); $lrc = 'http://m.kugou.com/app/i/krc.php?cmd=100&keyword='.$Music['fileName'].'&hash='.$Music['hash'].'&timelength='.$Music['timeLength']*1000; if(!isset($Music['error'])) { return ReturnMusicData($Music['url'], $Music['fileName'], $MusicGb, 'kugou_'.$Music['hash'], $lrc, $auth, '酷狗音乐', $Music['timeLength'], $Music['extName'], $Music['fileSize']); } else { ajaxReturn(array('code' => -1, 'message' => '歌曲未找到到')); } } /*获取歌手头像*/ public function GetAuthImg($key){ $url = 'http://m.kugou.com/app/i/getSingerHead_new.php?singerName='.key; $temp = file_get_contents($url); $info = json_decode($temp,true); if (isset($info['url'])) { return $info['url']; }else{ return ''; } } } |
为什么我现在又不想用它了?
近期一段时间由于某些原因,喜欢上了一首歌
没错就是这首歌
QQ音乐没有版权,听不了,于是,我拆卸了QQ音乐………..
网易云音乐提示您所在的地区咱不能播放此歌曲,于是,我拆卸了网易云音乐….
酷狗音乐也没有…..于是我…
正在努力踩点虾米音乐….因为虾米音乐有这首歌…..
好了,这真是个悲伤的故事…..
很棒的