代码很简单,不多b。

请求参数 [GET/POST]

  • id(必填:网易云歌曲id)
  • redirect(选填:是否直接重定向至资源。传入music则重定向至音乐,传入cover则重定向至歌曲封面。传入其它值无效。)
  • x(选填:获取的歌曲封面的长度,以px为单位。redirect为music时此参数无效。x或y其中一项为空时x、y均无效。默认值130。)
  • y(选填:获取的歌曲封面的宽度,以px为单位。redirect为music时此参数无效。x或y其中一项为空时x、y均无效。默认值130。)

返回格式 [JSON/STREAM]

  • id(传入的歌曲id)
  • name(歌曲名称)
  • author(作者名称)
  • musicurl(歌曲标清直链)
  • imgurl(封面直链)

若传入redirect且参数有效,则直接返回Location头重定向至对应资源。

请求示例

1.[GET]http://domain/netease.php?id=454730052
返回:
{"id":"454730052","name":"Starlight","author":"Starset","musicurl":"http://music.163.com/song/media/outer/url?id=454730052.mp3","imgurl":"http://p1.music.126.net/MYIl1dPJCMt0ay9zmIJRtQ==/17783501068032491.jpg?param=130y130"}

2.[GET]http://domain/netease.php?id=454730052&x=500&y=500
返回:
{"id":"454730052","name":"Starlight","author":"Starset","musicurl":"http://music.163.com/song/media/outer/url?id=454730052.mp3","imgurl":"http://p1.music.126.net/MYIl1dPJCMt0ay9zmIJRtQ==/17783501068032491.jpg?param=500y500"}

3.[GET]http://domain/netease.php?id=454730052&redirect=music
返回:(无)
返回头部:Location: http://music.163.com/song/media/outer/url?id=454730052.mp3

4.[GET]http://domain/netease.php?id=454730052&x=500&y=500&redirect=cover
返回:(无)
返回头部:Location: http://p1.music.126.net/MYIl1dPJCMt0ay9zmIJRtQ==/17783501068032491.jpg?param=500y500

代码

/*
 * NetEaseMusicParser
 * Author:LtD0gE
 * https://www.littledoge.top
 */
function getSubStr($str, $leftStr, $rightStr)
{
    $left = strpos($str, $leftStr);
    $right = strpos($str, $rightStr,$left);
    if($left < 0 or $right < $left) return '';
    return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr));
}
$id=$_REQUEST['id']; //use $_REQUEST to receive GET or POST data
$redirect=$_REQUEST['redirect'];
$x=$_REQUEST['x'];
$y=$_REQUEST['y'];
$netease_fullurl='https://music.163.com/m/song?id='.$id;
$musichtml=file_get_contents($netease_fullurl);
$imgurl=getSubStr($musichtml,'<img src="','" class="j-img"');
$musicurl='http://music.163.com/song/media/outer/url?id='.$id.'.mp3';
$name=getSubStr($musichtml,'<meta property="og:title" content="','" />');
$author=getSubStr($musichtml,'<p class="des s-fc4">歌手:<span title="','"><a class="');
if($x!='' || $y!='' || $redirect=='music'){
    $imgurl=str_replace('?param=130y130','?param='.$x.'y'.$y,$imgurl);
}
if($redirect=='music'){
    header('Location: '.$musicurl);
    exit();
}elseif($redirect=='cover'){
    header('Location: '.$imgurl);
    exit();
}
$rtnarray=array();
$rtnarray['id']=$id;
$rtnarray['name']=$name;
$rtnarray['author']=$author;
$rtnarray['musicurl']=$musicurl;
$rtnarray['imgurl']=$imgurl;
$rtnjson=json_encode($rtnarray,JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE);
$rtnjson=str_replace('http','https',$rtnjson);
exit($rtnjson);