前盐
网易云网页版自带 API,但是点击字后会自动跳转到 mp3 资源播放,这样很不屌.于是咱们就封装一个吧.
API
网易云的 API 为
我这个主题会把网易云的音乐链接自动变成 MP3 所以弄个图片
源码
新建个 PHP 文件.
<?php $id = @$_GET['id']; if (empty($id)) { exit(jsonm(['code' => 0, 'msg' => '请填写音乐 ID'])); } // 正则判断 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.kieng.cn'])); } else { $play = $arr[1][0]; $play = str_replace('http://', 'https://', trim($play) . '.mp3'); exit(jsonm(['code' => 0, 'msg' => '解析成功', 'play' => $play, 'referer' => 'https://blog.kieng.cn'])); } //处理成 https 格式 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)); }
调用:
看图吧