$v) { $this->info[$k] = $v; } $this->ParameterException(); } protected function ParameterException() { $id = $this->info['id']; if (!is_numeric($id)) { $this->Array = ['code' => -1, 'msg' => '请输入正确的歌单id']; $this->returns(); return; } $this->getPlaylistSongs(); } protected function getPlaylistSongs() { $id = $this->info['id']; $post_data = http_build_query(['s' => '100', 'id' => $id, 'n' => '100', 't' => '100']); $url = 'http://music.163.com/api/v6/playlist/detail'; $data = NetEaseHelper::curl($url, [ 'post' => $post_data, 'ua' => 'NeteaseMusic/8.6.31.211201171945(8006031);Dalvik/2.1.0 (Linux; U; Android 11; PCLM10 Build/RKQ1.200928.002)', 'Header' => $this->header ]); $decoded = json_decode($data, true); if (empty($decoded['playlist']['trackIds'])) { $this->Array = ['code' => -1, 'msg' => '无法获取歌单信息']; $this->returns(); return; } $array = $decoded['playlist']['trackIds']; $rand_song = $array[array_rand($array, 1)]; $this->id = $rand_song['id']; $this->getSongDetail(); } protected function getSongDetail() { $id = $this->id; $url = 'http://music.163.com/api/song/detail/?id=' . $id . '&ids=%5B' . $id . '%5D&csrf_token='; $data = json_decode(NetEaseHelper::curl($url), true); if (empty($data['songs'][0])) { $this->Array = ['code' => -1, 'msg' => '无法获取歌曲信息']; $this->returns(); return; } $song = $data['songs'][0]; $song_name = $song['name'] ?? '未知'; $artist_name = $song['artists'][0]['name'] ?? '未知'; $cover = $song['album']['picUrl'] ?? ''; $music_url = 'http://music.163.com/song/media/outer/url?id=' . $id; $tail = $this->info['tail'] ?? '网易云音乐'; $this->Array = [ 'code' => 200, 'msg' => '获取成功', 'data' => [ 'id' => $id, 'song' => $song_name, 'singer' => $artist_name, 'cover' => $cover, 'music_url' => $music_url, 'tail' => $tail ] ]; $this->returns(); } protected function returns() { $type = $this->info['type'] ?? 'json'; switch ($type) { case 'text': if (isset($this->Array['data'])) { $text = "歌曲:" . $this->Array['data']['song'] . "\n"; $text .= "歌手:" . $this->Array['data']['singer']; echo $text; } else { echo $this->Array['msg']; } break; case 'url': if (isset($this->Array['data']['music_url'])) { header('Location: ' . $this->Array['data']['music_url']); } else { echo json_encode($this->Array, JSON_UNESCAPED_UNICODE); } break; default: echo json_encode($this->Array, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); break; } exit; } } // 获取参数 $songId = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0; $tail = isset($_REQUEST['tail']) ? $_REQUEST['tail'] : '网易云音乐'; $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'json'; // 如果没提供歌单ID,使用默认热门歌单 if (empty($songId) || $songId <= 0) { $playlists = [71385702, 2884035, 3778678, 3779629, 19723756]; $songId = $playlists[array_rand($playlists, 1)]; } // 执行查询 new Music_163_Rand([ 'id' => $songId, 'type' => $type, 'tail' => $tail ]); ?>