ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
首先要获取hash值是歌曲名的hash值 搜索歌曲接口 https://songsearch.kugou.com/song_search_v2?keyword=numb 关键词 得到一个lists列表数据 歌曲的hash值 FileHash 和文件名 第二 获取歌曲播放地址接口 mid不知道是什么意思 猜测是浏览器的编码还是什么鬼 以下是示例接口 https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash=A9DC61CBFBC27F1F0BF63482D9469438&mid=407a9697947211b828f908950a4460ec https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash=29AFE0B705056DE252ED0E5A6E4A5EE2&mid=407a9697947211b828f908950a4460ec 得到播放地址 play_url 此为歌曲播放地址 第三下载歌曲 可以替换名字 以下为测试代码 使用第三方库 ``` composer require curl/curl ``` ``` <?php namespace app\index\controller; use Curl\Curl; use think\Controller; use think\Request; class Index extends Controller { public function test(Request $request) { $keyword = $request->param('keyword'); $url = "https://songsearch.kugou.com/song_search_v2"; $curl = new Curl(); $arr = ['keyword' => $keyword]; $curl->get($url,$arr); $curl->close(); $result = $curl->response; $res = json_decode($result, true); $lists = $res['data']['lists']; $arr = []; foreach ($lists as $k => $v) { // 过滤数据 // $arr[$k]['name'] = $v['SongName']; // $arr[$k]['hash'] = $v['FileHash']; $this->getPlayUrl($v['FileHash'], $v['SongName']); } // 是否存数据库 待定 // return json($arr); } public function getPlayUrl($hash, $song_name) { // $url = "https://wwwapi.kugou.com/yy/index.php?r=play/getdata&mid=407a9697947211b828f908950a4460ec&hash=A8136351BE3DA7D077E95B976EB723CB"; $url = "https://wwwapi.kugou.com/yy/index.php"; $curl = new Curl(); $curl->setOpt(CURLOPT_RETURNTRANSFER, FALSE); // https接口需设置这个为false $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE); // https接口需设置这个为false $curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE); // https接口需设置这个为false $curl->setOpt(CURLOPT_RETURNTRANSFER, 1); // 只获取不显示 $arr = [ 'r' =>'play/getdata', 'mid' =>'407a9697947211b828f908950a4460ec', 'hash' =>$hash, ]; $curl->get($url,$arr); $curl->close(); $result = $curl->response; $res = json_decode($result, true); $play_url = $res['data']['play_url'];// 音乐播放地址 $this->down($play_url, $song_name); } public function down($playUrl, $song_name) { $ch = new Curl(); $curl->setOpt(CURLOPT_RETURNTRANSFER, FALSE); // https接口需设置这个为false $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE); // https接口需设置这个为false $curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE); // https接口需设置这个为false $curl->setOpt(CURLOPT_RETURNTRANSFER, 1); // 只获取不显示 $ch->get($playUrl); $ch->close(); // 打开文件夹 $path = '/www/wwwroot/www.kg.com/public/'; // 写入文件处理 $file_name = $song_name . '.mp3'; $fp = fopen($path . $file_name, 'w+'); fwrite($fp, $ch->response); } } ```