diff --git a/README.md b/README.md index 7d85bba..bafcaa4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,249 @@ -# Neteaseflc_php -网易云无损解析PHP源码 +# 网易云音乐 API - PHP 版本 + +![PHP](https://img.shields.io/badge/PHP-7.0%2B-blue.svg) +![License](https://img.shields.io/badge/license-MIT-green.svg) +![Version](https://img.shields.io/badge/version-1.0.0-orange.svg) + +一个功能完整的网易云音乐 API PHP 实现,支持获取音乐播放链接、歌曲详情、歌词、搜索、歌单和专辑信息等功能。 + +## ✨ 特性 + +- 🎵 **音乐播放链接获取** - 支持多种音质(标准、高品质、无损、Hi-Res等) +- 🔍 **音乐搜索** - 支持关键词搜索歌曲 +- 📝 **歌词获取** - 支持普通歌词、翻译歌词、罗马音歌词 +- 📀 **歌曲详情** - 获取歌曲完整信息 +- 📋 **歌单解析** - 获取歌单详情及所有歌曲列表 +- 💿 **专辑解析** - 获取专辑详情及所有歌曲列表 +- 🍪 **Cookie 支持** - 支持登录状态获取更多权限 +- 🔐 **加密请求** - 使用网易云官方加密算法 + +## 📋 系统要求 + +- PHP 7.0 或更高版本 +- cURL 扩展 +- OpenSSL 扩展 + +## 🚀 快速开始 + +### 1. 下载项目 + +```bash +git clone https://github.com/your-username/netease-music-api-php.git +cd netease-music-api-php/demophp +``` + +### 2. 配置 Cookie(可选) + +创建 `cookie.txt` 文件并添加你的网易云音乐 Cookie: + +``` +MUSIC_U=your_music_u_value; __csrf=your_csrf_value; ... +``` + +### 3. 使用示例 + +```php +loadCookieFromFile('cookie.txt'); + +// 获取音乐播放链接 +$musicUrl = $api->getMusicUrl('1315196858', 'standard', $cookies); +echo json_encode($musicUrl, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); +?> +``` + +## 📖 API 文档 + +### 核心类:NeteaseMusicAPI + +#### 构造函数 + +```php +$api = new NeteaseMusicAPI($cookies = []); +``` + +#### 主要方法 + +##### 1. 获取音乐播放链接 + +```php +$result = $api->getMusicUrl($id, $level = 'standard', $cookies = []); +``` + +**参数:** +- `$id` (string|array): 歌曲ID,支持单个ID或多个ID数组 +- `$level` (string): 音质等级 + - `standard`: 标准音质 + - `exhigh`: 极高音质 + - `lossless`: 无损音质 + - `hires`: Hi-Res音质 + - `jyeffect`: 高清环绕声 + - `sky`: 沉浸环绕声 + - `jymaster`: 超清母带 +- `$cookies` (array): Cookie数组 + +**示例:** +```php +// 单个歌曲 +$result = $api->getMusicUrl('1315196858', 'lossless', $cookies); + +// 多个歌曲 +$result = $api->getMusicUrl(['1315196858', '119667185'], 'standard', $cookies); +``` + +##### 2. 搜索音乐 + +```php +$result = $api->getSearchMusic($keywords, $limit = 10, $offset = 0, $cookies = []); +``` + +**参数:** +- `$keywords` (string): 搜索关键词 +- `$limit` (int): 每页数量 +- `$offset` (int): 偏移量 +- `$cookies` (array): Cookie数组 + +##### 3. 获取歌曲详情 + +```php +$result = $api->getSongDetail($id, $cookies = []); +``` + +##### 4. 获取歌词 + +```php +$result = $api->getLyric($id, $cookies = []); +``` + +##### 5. 获取歌单详情 + +```php +$result = $api->getPlaylistDetail($playlistId, $cookies = []); +``` + +##### 6. 获取专辑详情 + +```php +$result = $api->getAlbumDetail($albumId, $cookies = []); +``` + +##### 7. Cookie 管理 + +```php +// 从文件加载 Cookie +$cookies = $api->loadCookieFromFile('cookie.txt'); + +// 设置 Cookie +$api->setCookies($cookies); + +// 获取当前 Cookie +$currentCookies = $api->getCookies(); +``` + +## 📁 项目结构 + +``` +demophp/ +├── getMusicapi.php # 核心 API 类 +├── getMusicUrl.php # 获取音乐链接示例 +├── getSearchMusic.php # 搜索音乐示例 +├── getSongDetail.php # 获取歌曲详情示例 +├── getLyric.php # 获取歌词示例 +├── getPlaylistDetail.php # 获取歌单详情示例 +├── getAlbumDetail.php # 获取专辑详情示例 +├── cookie.txt # Cookie 配置文件 +└── README.md # 项目说明文档 +``` + +## 🔧 使用示例 + +### 获取音乐播放链接 + +```php +loadCookieFromFile('cookie.txt'); + +// 获取无损音质 +$result = $api->getMusicUrl('1315196858', 'lossless', $cookies); + +foreach ($result['data'] as $song) { + echo "歌曲ID: " . $song['id'] . "\n"; + echo "播放链接: " . $song['url'] . "\n"; + echo "音质: " . $song['level'] . "\n"; + echo "比特率: " . $song['br'] . "\n"; + echo "文件大小: " . $song['size'] . "\n"; +} +?> +``` + +### 搜索音乐 + +```php +loadCookieFromFile('cookie.txt'); + +$result = $api->getSearchMusic('薛之谦', 20, 0, $cookies); + +echo "搜索结果总数: " . $result['total'] . "\n"; +foreach ($result['songs'] as $song) { + echo "歌曲: " . $song['name'] . " - " . $song['artists'] . "\n"; + echo "专辑: " . $song['album'] . "\n"; + echo "ID: " . $song['id'] . "\n\n"; +} +?> +``` + +### 获取歌词 + +```php +loadCookieFromFile('cookie.txt'); + +$result = $api->getLyric('1315196858', $cookies); + +echo "原文歌词:\n" . $result['lrc']['lyric'] . "\n"; +echo "翻译歌词:\n" . $result['tlyric']['lyric'] . "\n"; +?> +``` + +## ⚠️ 注意事项 + +1. **Cookie 获取**:部分功能需要登录状态,请从浏览器开发者工具中获取 Cookie +2. **请求频率**:请合理控制请求频率,避免被服务器限制 +3. **版权声明**:本项目仅供学习交流使用,请尊重音乐版权 +4. **稳定性**:网易云音乐 API 可能会发生变化,如遇问题请及时更新 + +## 🤝 贡献 + +欢迎提交 Issue 和 Pull Request 来改进这个项目! + +## 📄 许可证 + +本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。 + +## 👨‍💻 作者 + +- **苏晓晴** - [www.toubiec.cn](https://www.toubiec.cn) + +## 🙏 致谢 + +感谢网易云音乐提供的优质音乐服务,本项目仅用于技术学习和交流。 + +--- + +⭐ 如果这个项目对你有帮助,请给它一个 Star! \ No newline at end of file diff --git a/cookie.txt b/cookie.txt new file mode 100644 index 0000000..3c64d8e --- /dev/null +++ b/cookie.txt @@ -0,0 +1 @@ +MUSIC_U=;os=pc;appver=8.9.75; \ No newline at end of file diff --git a/getAlbumDetail.php b/getAlbumDetail.php new file mode 100644 index 0000000..f013021 --- /dev/null +++ b/getAlbumDetail.php @@ -0,0 +1,20 @@ +getAlbumDetail($singleId); +echo json_encode($rawResult,480); +?> \ No newline at end of file diff --git a/getLyric.php b/getLyric.php new file mode 100644 index 0000000..852b7c4 --- /dev/null +++ b/getLyric.php @@ -0,0 +1,29 @@ +loadCookieFromFile($cookieFile); + +$singleId = '2006535009'; // 歌曲ID 支持单ID 例如2006535009 + +$rawResult = $api->getLyric($singleId, $cookies); +$lyric['lrc'] = $rawResult['lrc']['lyric'] ?? ''; // 歌词 +$lyric['tlyric'] = $rawResult['tlyric']['lyric'] ?? ''; // 翻译歌词 +$lyric['romalrc'] = $rawResult['romalrc']['lyric'] ?? ''; // 罗马音歌词 +$lyric['klyric'] = $rawResult['klyric']['lyric'] ?? ''; // 滚动歌词 +$list['data'] = $lyric; +echo json_encode($list, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); +?> \ No newline at end of file diff --git a/getMusicUrl.php b/getMusicUrl.php new file mode 100644 index 0000000..bb89e03 --- /dev/null +++ b/getMusicUrl.php @@ -0,0 +1,34 @@ +loadCookieFromFile($cookieFile); + +$singleId = '26608738,212233'; // 歌曲ID 支持单ID/多ID 例如30500857 1315196858,1888667593,30500857 +$quality = 'jymaster'; // 音质支持 standard, exhigh, lossless, hires, jyeffect, sky, jymaster +$rawResult = $api->getMusicUrl($singleId, $quality, $cookies); +foreach ($rawResult['data'] as $urls) { + $temp = []; + $temp['id'] = $urls['id']; + $temp['url'] = str_replace("http://", "https://", $urls['url']); + $temp['br'] = $urls['br']; + $temp['level'] = $urls['level']; + $temp['size'] = $urls['size']; + $temp['md5'] = $urls['md5']; + $list[] = $temp; +} +echo json_encode($list, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); +?> \ No newline at end of file diff --git a/getMusicapi.php b/getMusicapi.php new file mode 100644 index 0000000..4f2ce68 --- /dev/null +++ b/getMusicapi.php @@ -0,0 +1,529 @@ +cookies = array_merge([ + 'os' => 'pc', + 'appver' => '', + 'osver' => '', + 'deviceId' => 'pyncm!' + ], $cookies); + } + + /** + * 十六进制编码 + */ + private function hexDigest($data) { + return bin2hex($data); + } + + /** + * MD5哈希 + */ + private function hashDigest($text) { + return md5($text, true); + } + + /** + * MD5哈希十六进制 + */ + private function hashHexDigest($text) { + return $this->hexDigest($this->hashDigest($text)); + } + + /** + * PKCS7填充 + */ + private function pkcs7Pad($data, $blockSize) { + $pad = $blockSize - (strlen($data) % $blockSize); + return $data . str_repeat(chr($pad), $pad); + } + + /** + * AES加密 + */ + private function aesEncrypt($data, $key = null) { + if ($key === null) { + $key = $this->aesKey; + } + $padded = $this->pkcs7Pad($data, 16); + return openssl_encrypt($padded, 'AES-128-ECB', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); + } + + /** + * HTTP POST请求 + */ + private function post($url, $params, $cookies = []) { + $allCookies = array_merge($this->cookies, $cookies); + + $cookieStr = ''; + foreach ($allCookies as $key => $value) { + $cookieStr .= $key . '=' . $value . '; '; + } + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['params' => $params])); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'User-Agent: ' . $this->userAgent, + 'Referer: ', + 'Cookie: ' . rtrim($cookieStr, '; ') + ]); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($response === false || $httpCode !== 200) { + throw new Exception('HTTP请求失败: ' . $httpCode); + } + + return $response; + } + + /** + * 普通HTTP POST请求 + */ + private function simplePost($url, $data, $cookies = []) { + $allCookies = array_merge($this->cookies, $cookies); + + $cookieStr = ''; + foreach ($allCookies as $key => $value) { + $cookieStr .= $key . '=' . $value . '; '; + } + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'User-Agent: ' . $this->userAgent, + 'Referer: https://music.163.com/', + 'Cookie: ' . rtrim($cookieStr, '; ') + ]); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + + $response = curl_exec($ch); + curl_close($ch); + + return $response; + } + + /** + * 普通HTTP GET请求 + */ + private function simpleGet($url, $cookies = []) { + $allCookies = array_merge($this->cookies, $cookies); + + $cookieStr = ''; + foreach ($allCookies as $key => $value) { + $cookieStr .= $key . '=' . $value . '; '; + } + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'User-Agent: ' . $this->userAgent, + 'Referer: https://music.163.com/', + 'Cookie: ' . rtrim($cookieStr, '; ') + ]); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($response === false || $httpCode !== 200) { + throw new Exception('HTTP GET请求失败: ' . $httpCode); + } + + return $response; + } + + /** + * 获取音乐播放URL-V1 + * @param string|array $id 单个ID或多个ID数组,例如: '1315196858' 或 ['1315196858', '119667185'] + * @param string $level 音质等级 + * @param array $cookies Cookie数组 + */ + public function getMusicUrl($id, $level = 'standard', $cookies = []) { + $url = 'https://interface3.music.163.com/eapi/song/enhance/player/url/v1'; + + $config = [ + 'os' => 'pc', + 'appver' => '', + 'osver' => '', + 'deviceId' => 'pyncm!', + 'requestId' => (string)rand(20000000, 30000000) + ]; + // 处理单个ID或多个ID + $ids = is_array($id) ? $id : [$id]; + + $payload = [ + 'ids' => $ids, + 'level' => $level, + 'encodeType' => 'flac', + 'header' => json_encode($config) + ]; + + if ($level === 'sky') { + $payload['immerseType'] = 'c51'; + } + + $url2 = str_replace('/eapi/', '/api/', parse_url($url, PHP_URL_PATH)); + $digest = $this->hashHexDigest('nobody' . $url2 . 'use' . json_encode($payload) . 'md5forencrypt'); + $params = $url2 . '-36cd479b6b5-' . json_encode($payload) . '-36cd479b6b5-' . $digest; + + $encrypted = $this->aesEncrypt($params); + $encryptedHex = $this->hexDigest($encrypted); + + $response = $this->post($url, $encryptedHex, $cookies); + return json_decode($response, true); + } + + /** + * 获取歌曲详情-获取 + * @param string $id 歌曲ID + * @param array $cookies Cookie数组 + */ + public function getSongDetail($id, $cookies = []) { + $url = 'https://interface3.music.163.com/eapi/v3/song/detail'; + + $config = [ + 'os' => 'pc', + 'appver' => '', + 'osver' => '', + 'deviceId' => 'pyncm!', + 'requestId' => (string)rand(20000000, 30000000) + ]; + + $payload = [ + 'c' => json_encode([['id' => (int)$id, 'v' => 0]]), + 'header' => json_encode($config) + ]; + + $url2 = str_replace('/eapi/', '/api/', parse_url($url, PHP_URL_PATH)); + $digest = $this->hashHexDigest('nobody' . $url2 . 'use' . json_encode($payload) . 'md5forencrypt'); + $params = $url2 . '-36cd479b6b5-' . json_encode($payload) . '-36cd479b6b5-' . $digest; + + $encrypted = $this->aesEncrypt($params); + $encryptedHex = $this->hexDigest($encrypted); + + $response = $this->post($url, $encryptedHex, $cookies); + return json_decode($response, true); + } + + /** + * 获取歌词-获取 + * @param string $id 歌曲ID + * @param array $cookies Cookie数组 + */ + public function getLyric($id, $cookies = []) { + $url = 'https://interface3.music.163.com/eapi/song/lyric'; + + $config = [ + 'os' => 'pc', + 'appver' => '', + 'osver' => '', + 'deviceId' => 'pyncm!', + 'requestId' => (string)rand(20000000, 30000000) + ]; + + $payload = [ + 'id' => $id, + 'cp' => 'false', + 'tv' => '0', + 'lv' => '0', + 'rv' => '0', + 'kv' => '0', + 'yv' => '0', + 'ytv' => '0', + 'yrv' => '0', + 'header' => json_encode($config) + ]; + + $url2 = str_replace('/eapi/', '/api/', parse_url($url, PHP_URL_PATH)); + $digest = $this->hashHexDigest('nobody' . $url2 . 'use' . json_encode($payload) . 'md5forencrypt'); + $params = $url2 . '-36cd479b6b5-' . json_encode($payload) . '-36cd479b6b5-' . $digest; + + $encrypted = $this->aesEncrypt($params); + $encryptedHex = $this->hexDigest($encrypted); + + $response = $this->post($url, $encryptedHex, $cookies); + return json_decode($response, true); + } + + /** + * 搜索音乐-获取 + * @param string $keywords 搜索关键词 + * @param int $limit 每页数量 + * @param int $offset 偏移量 + * @param array $cookies Cookie数组 + */ + public function getSearchMusic($keywords, $limit = 10, $offset = 0, $cookies = []) { + $url = 'https://interface3.music.163.com/eapi/cloudsearch/pc'; + + $config = [ + 'os' => 'pc', + 'appver' => '', + 'osver' => '', + 'deviceId' => 'pyncm!', + 'requestId' => (string)rand(20000000, 30000000) + ]; + + $payload = [ + 's' => $keywords, + 'type' => 1, + 'limit' => $limit, + 'offset' => $offset, + 'header' => json_encode($config) + ]; + + $url2 = str_replace('/eapi/', '/api/', parse_url($url, PHP_URL_PATH)); + $digest = $this->hashHexDigest('nobody' . $url2 . 'use' . json_encode($payload) . 'md5forencrypt'); + $params = $url2 . '-36cd479b6b5-' . json_encode($payload) . '-36cd479b6b5-' . $digest; + + $encrypted = $this->aesEncrypt($params); + $encryptedHex = $this->hexDigest($encrypted); + + $response = $this->post($url, $encryptedHex, $cookies); + $result = json_decode($response, true); + + $songs = []; + if (isset($result['result']['songs'])) { + foreach ($result['result']['songs'] as $item) { + $artists = []; + foreach ($item['ar'] as $artist) { + $artists[] = $artist['name']; + } + + $songs[] = [ + 'id' => $item['id'], + 'name' => $item['name'], + 'artists' => implode('/', $artists), + 'album' => $item['al']['name'], + 'picUrl' => $item['al']['picUrl'], + 'duration' => $item['dt'] ?? 0 + ]; + } + } + + return [ + 'songs' => $songs, + 'total' => $result['result']['songCount'] ?? 0 + ]; + } + + /** + * 获取歌单详情 + */ + public function getPlaylistDetail($playlistId, $cookies = []) { + $url = 'https://music.163.com/api/v6/playlist/detail'; + + $payload = [ + 'id' => $playlistId + ]; + + $response = $this->simplePost($url, $payload, $cookies); + $result = json_decode($response, true); + + if (!isset($result['playlist'])) { + return null; + } + + $playlist = $result['playlist']; + $info = [ + 'id' => $playlist['id'] ?? null, + 'name' => $playlist['name'] ?? '', + 'coverImgUrl' => $playlist['coverImgUrl'] ?? '', + 'creator' => $playlist['creator']['nickname'] ?? '', + 'trackCount' => $playlist['trackCount'] ?? 0, + 'description' => $playlist['description'] ?? '', + 'tracks' => [] + ]; + + // 获取所有trackIds + $trackIds = []; + if (isset($playlist['trackIds']) && is_array($playlist['trackIds'])) { + foreach ($playlist['trackIds'] as $track) { + $trackIds[] = (string)$track['id']; + } + } + + // 分批获取详细信息(每批最多100首) + for ($i = 0; $i < count($trackIds); $i += 100) { + $batchIds = array_slice($trackIds, $i, 100); + + foreach ($batchIds as $id) { + // 直接调用getSongDetail方法 + $songResult = $this->getSongDetail($id, $cookies); + + if (isset($songResult['songs']) && is_array($songResult['songs'])) { + foreach ($songResult['songs'] as $song) { + $artists = []; + if (isset($song['ar']) && is_array($song['ar'])) { + foreach ($song['ar'] as $artist) { + $artists[] = $artist['name']; + } + } + + $info['tracks'][] = [ + 'id' => $song['id'], + 'name' => $song['name'], + 'artists' => implode('/', $artists), + 'album' => $song['al']['name'] ?? '', + 'picUrl' => $song['al']['picUrl'] ?? '' + ]; + } + } + } + } + + return $info; + } + + /** + * 获取网易云专辑详情及全部歌曲列表 + * @param string $albumId 专辑ID + * @param array $cookies 登录cookies + * @return array 专辑基本信息和全部歌曲列表 + */ + public function getAlbumDetail($albumId, $cookies = []) { + $url = "https://music.163.com/api/v1/album/{$albumId}"; + + $response = $this->simpleGet($url, $cookies); + $result = json_decode($response, true); + + if (!isset($result['album'])) { + return null; + } + + $album = $result['album']; + + $info = [ + 'id' => $album['id'] ?? null, + 'name' => $album['name'] ?? '', + 'coverImgUrl' => isset($album['pic']) ? $this->getPicUrl($album['pic']) : '', + 'artist' => $album['artist']['name'] ?? '', + 'publishTime' => $album['publishTime'] ?? null, + 'description' => $album['description'] ?? '', + 'songs' => [] + ]; + + // 处理专辑中的歌曲列表 + if (isset($result['songs']) && is_array($result['songs'])) { + foreach ($result['songs'] as $song) { + // 提取艺术家信息 + $artists = []; + if (isset($song['ar']) && is_array($song['ar'])) { + foreach ($song['ar'] as $artist) { + $artists[] = $artist['name']; + } + } + + $info['songs'][] = [ + 'id' => $song['id'], + 'name' => $song['name'], + 'artists' => implode('/', $artists), + 'album' => $song['al']['name'] ?? '', + 'picUrl' => isset($song['al']['pic']) ? $this->getPicUrl($song['al']['pic']) : '' + ]; + } + } + + return $info; + } + + /** + * 网易云图片ID加密 + */ + public function encryptId($idStr) { + $magic = '3go8&$8*3*3h0k(2)2'; + $songId = str_split($idStr); + + for ($i = 0; $i < count($songId); $i++) { + $songId[$i] = chr(ord($songId[$i]) ^ ord($magic[$i % strlen($magic)])); + } + + $m = implode('', $songId); + $md5Bytes = md5($m, true); + $result = base64_encode($md5Bytes); + $result = str_replace(['/', '+'], ['_', '-'], $result); + + return $result; + } + + /** + * 获取图片URL + */ + public function getPicUrl($picId, $size = 300) { + $encId = $this->encryptId((string)$picId); + return "https://p3.music.126.net/{$encId}/{$picId}.jpg?param={$size}y{$size}"; + } + + /** + * 从cookie.txt文件读取cookie + * @param string $cookieFile cookie文件路径 + * @return array cookie数组 + */ + public function loadCookieFromFile($cookieFile) { + if (!file_exists($cookieFile)) { + throw new Exception("Cookie文件不存在: {$cookieFile}"); + } + + $cookieContent = file_get_contents($cookieFile); + if ($cookieContent === false) { + throw new Exception("无法读取Cookie文件: {$cookieFile}"); + } + + // 清理cookie字符串,移除换行符和多余空格 + $cookieContent = trim($cookieContent); + $cookieContent = str_replace(["\r\n", "\n", "\r"], '', $cookieContent); + // 将cookie字符串解析为数组 + $cookies = []; + $pairs = explode(';', $cookieContent); + foreach ($pairs as $pair) { + $pair = trim($pair); + if (strpos($pair, '=') !== false) { + list($key, $value) = explode('=', $pair, 2); + $cookies[trim($key)] = trim($value); + } + } + return $cookies; + } + + /** + * 设置Cookie + */ + public function setCookies($cookies) { + $this->cookies = array_merge($this->cookies, $cookies); + } + + /** + * 获取当前Cookie + */ + public function getCookies() { + return $this->cookies; + } +} +?> \ No newline at end of file diff --git a/getPlaylistDetail.php b/getPlaylistDetail.php new file mode 100644 index 0000000..8e380be --- /dev/null +++ b/getPlaylistDetail.php @@ -0,0 +1,27 @@ +getPlaylistDetail($singleId); +$getPlaylistDetail['id'] = $rawResult['id'] ?? ''; // 歌单ID +$getPlaylistDetail['name'] = $rawResult['name'] ?? ''; // 歌单名称 +$getPlaylistDetail['coverImgUrl'] = $rawResult['coverImgUrl'] ?? ''; // 歌单封面 +$getPlaylistDetail['trackCount'] = $rawResult['trackCount'] ?? ''; // 歌单歌曲数量 +$getPlaylistDetail['creator'] = $rawResult['creator'] ?? ''; // 歌单创建者 +$getPlaylistDetail['tracks'] = $rawResult['tracks'] ?? ''; // 歌曲列表 +$list['data'] = $getPlaylistDetail; +echo json_encode($list, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); +?> \ No newline at end of file diff --git a/getSearchMusic.php b/getSearchMusic.php new file mode 100644 index 0000000..91680e4 --- /dev/null +++ b/getSearchMusic.php @@ -0,0 +1,26 @@ +loadCookieFromFile($cookieFile); + +$search = '薛之谦'; // 搜索内容 +$limit = 100; // 搜索数量 +$offset = 0; // 搜索偏移量 + +$rawResult = $api->getSearchMusic($search, $limit, $offset, $cookies); +exit(json_encode($rawResult, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); +?> \ No newline at end of file diff --git a/getSongDetail.php b/getSongDetail.php new file mode 100644 index 0000000..4af1f6a --- /dev/null +++ b/getSongDetail.php @@ -0,0 +1,35 @@ +loadCookieFromFile($cookieFile); + +$singleId = '2006535009'; // 歌曲ID 支持单ID 例如2006535009 + +$rawResult = $api->getSongDetail($singleId, $cookies); + +$songdetail['id'] = $rawResult['songs'][0]['id'] ?? ''; // 歌曲ID +$songdetail['name'] = $rawResult['songs'][0]['name'] ?? ''; // 歌曲名 +$songdetail['album'] = $rawResult['songs'][0]['al']['name'] ?? ''; // 专辑 +foreach ($rawResult['songs'][0]['ar'] as $artist) { + $artists[] = $artist['name']; +} +$artistString = implode('/', $artists); +$songdetail['singer'] = $artistString ?? ''; // 歌手 +$songdetail['picimg'] = $rawResult['songs'][0]['al']['picUrl'] ?? ''; // 专辑图片 +$list['data'] = $songdetail; +echo json_encode($list, 480); +?> \ No newline at end of file