网易云音乐直链解析API源代码

发布时间:2020-03-17 20:57:04 热度: 9249 ℃ 评论数: 1
$id = @$_GET['id'];
if (empty($id)) {
	exit(jsonm(['code' => 0, 'msg' => '请填写音乐 ID']));
}
if (!preg_match('/^[0-9]*$/', $id)) {
	exit(jsonm(['code' => 0, 'msg' => '这不是有效的音乐 ID']));
}
$api = 'http://music.163.com/song/media/outer/url?id=';
$url = $api . $id . '.mp3';
$data = curlGet($url);
preg_match_all('/Location:(.*?)\.mp3/', $data, $arr);
if (empty($arr[1][0])) {

exit(jsonm(['code' => 1, 'msg' => '解析失败', 'play' => null, 'referer' => 'https://blog.weihangapi.cn']));

} else { $play = $arr[1][0]; $play = str_replace('http://', 'https://', trim($play) . '.mp3');

exit(jsonm(['code' => 0, 'msg' => '解析成功', 'play' => $play, 'referer' => 'https://blog.weihangapi.cn']));

} function curlGet($url, $referurl = '') { if (empty($referurl)) { $referurl = $url; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // 输出头 curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //模拟常用浏览器的 useragent curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2'); // 不跟随跳转 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); //模拟来源网址 curl_setopt($ch, CURLOPT_REFERER, $referurl); $output = curl_exec($ch); curl_close($ch); return $output; } //json 美化 function jsonm($data) { header('content-type:application/json;charset=utf-8'); return stripslashes(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); }
吐槽一下
  • 消灭零回复

手机扫码访问