我做了一个简单的音乐播放器,歌曲数据都是来自网易云音乐。之前没找到API,所有歌曲信息都要自己手动添加到xml文件中。经过Misaka酱的帮助,使用了网易的API来接收json格式的歌曲信息,方便了很多。
先看看API:
获取单曲: http://music.163.com/api/song/detail/?id=28151022&ids=%5B28151022%5D 获取歌单: http://music.163.com/api/playlist/detail?id=15985919
现在我使用的是第二种方法,就是建立一个歌单,然后获取配置信息。因为它只有一个ajax请求,稳健性非常好。如果使用单曲一个个去请求的话,数量多了可能会出现失败的情况。
这样还有一个优点,那就是如果音乐有变动,不需要改文件了,歌单变了本地列表自然也会跟着变。只是如果要调整歌单内歌曲的顺序的话,得用网易云音乐的客户端才行。
好了废话不说,上代码。
js部分比较短,先看看吧:
$.ajax({
type: "get",
async: true,
url: "get163PlayListInfo.php?id=156380990",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: true,
success: function (json) {
//示例
var musiclength=json.result.tracks.length;
}
});
请求了get163PlayListInfo.php这个文件,后面的id是歌单id。你在网易云打开这个歌单的页面,就能在网址最后看到歌单id。
然后是php文件:
<?php
function netease_http($url)
{
$refer = "http://music.163.com/";
$header[] = "Cookie: " . "appver=1.5.0.75771;";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $refer);
$cexecute = curl_exec($ch);
curl_close($ch);
if ($cexecute) {
$result = $cexecute;
return $result;
}else{
return false;
}
}
?>
<?php
$playlist_id = $_REQUEST['id'];
$url = "http://music.163.com/api/playlist/detail?id=".$playlist_id ;
$response = netease_http($url);
echo $response; //此文件中没有设置编码,所以接收的网页上最好是uft-8编码
?>
Google Chrome 63
Windows 10/11
Google Chrome 53
Windows XP 遗憾的告诉你,已经不行了。
Google Chrome 47
Windows 10/11 把网易http://music.163.com/api代理到了我的网站下面,跨域就搞定了
还找到一个返回歌单的,我那200多首歌的歌单能有近1M的JSON...
你的20多个请求换成1个歌单试试
http://45.78.50.43/%E7%BD%91%E6%98%93%E4%BA%91%E9%9F%B3%E4%B9%90/api/playlist/detail?id=1136589
有没有获取歌名的