private static function get_netease_music($id, $type = 'song')
{
$return = false;
switch ($type) {
case 'song':
$url = "http://music.163.com/api/song/detail/?ids=[{$id}]";
$key = 'songs';
break;
case 'album':
$url = "http://music.163.com/api/album/{$id}?id={$id}";
$key = 'album';
break;
case 'artist':
$url = "http://music.163.com/api/artist/{$id}?id={$id}";
$key = 'artist';
break;
case 'collect':
$url = "http://music.163.com/api/playlist/detail?id={$id}";
$key = 'result';
break;
default:
$url = "http://music.163.com/api/song/detail/?ids=[{$id}]";
$key = 'songs';
}
if (!function_exists('curl_init')) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cookie: appver=2.0.2'));
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, 'http://music.163.com/;');
$cexecute = curl_exec($ch);
curl_close($ch);
if ($cexecute) {
$result = json_decode($cexecute, true);
if ($result['code'] == 200 && $result[$key]) {
$return['status'] = true;
$return['message'] = "";
switch ($key) {
case 'songs':
$data = $result[$key];
break;
case 'album':
$data = $result[$key]['songs'];
break;
case 'artist':
$data = $result['hotSongs'];
break;
case 'result':
$data = $result[$key]['tracks'];
break;
default:
$data = $result[$key];
break;
}
//列表
$list = array();
foreach ($data as $keys => $data) {
//获取歌词
$lyric = self::get_netease_lyric($data['id']);
if ($lyric) {
$lyric = $lyric['lyric'];
}
$list[$data['id']] = array('song_id' => $data['id'], 'title' => $data['name'], 'album_name' => $data['album']['name'], 'artist' => $data['artists'][0]['name'], 'location' => str_replace('http://m', 'http://p', $data['mp3Url']), 'pic' => $data['album']['blurPicUrl'] . '?param=106x106', 'lyric' => $lyric);
}
//修复一次添加多个id的乱序问题
if ($type = 'song' && strpos($id, ',')) {
$ids = explode(',', $id);
$r = array();
foreach ($ids as $v) {
if (!empty($list[$v])) {
$r[] = $list[$v];
}
}
$list = $r;
}
//最终播放列表
$return['trackList'] = $list;
}
} else {
$return = array('status' => false, 'message' => '非法请求');
}
return $return;
}