-
Notifications
You must be signed in to change notification settings - Fork 3
/
Api.php
388 lines (370 loc) · 10.4 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/**
* NeteaseCloudMusicApi
* @author Joseph Lee
*/
class Api {
const refer = 'http://music.163.com';
const api_url = 'http://music.163.com/api/';
const secret_bit_mask = '3go8&$8*3*3h0k(2)2';
const mp3_hd_url = "http://m1.music.126.net/";
const mp3_sd_url = "http://p1.music.126.net/";
/**
* 封装curl操作
* @param String $url 操作的URL
* @param string $data POST参数,默认空
* @return string 获得的JSON数据
*/
protected function request($url, $data = null) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_REFERER => self::refer]);
if ($data) {
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data]);
}
$html = curl_exec($ch);
curl_close($ch);
return json_decode($html, true);
}
/**
* 将String转换为byteArray
* @param String $string 传入字符串
* @return string byteArray
*/
protected function getBytes($string) {
$bytes = array();
$size = strlen($string);
for ($i = 0; $i < $size; $i++) {
$bytes[] = ord($string[$i]);
}
return $bytes;
}
/**
* 将byte数组转成ascii编码的字符串
* @param string $bytes byteArray
* @return String ascii字符串
*/
protected function toStr($bytes) {
$str = '';
$size = count($bytes);
for ($i = 0; $i < $size; $i++) {
$str .= chr($bytes[$i]);
}
// var_dump($str);
return $str;
}
/**
* 获得加密后的下载URL
* @param int $id SongId
* @return String mp3Url
*/
public function get_hd_mp3_url($id) {
return $this->get_mp3_url($id,"hd");
}
public function get_sd_mp3_url($id) {
return $this->get_mp3_url($id,"sd");
}
public function get_mp3_url($id, $type) {
$byte1[] = $this->getBytes(self::secret_bit_mask); //18
$byte2[] = $this->getBytes($id); //16
$magic = $byte1[0];
$song_id = $byte2[0];
$size = count($song_id);
for ($i = 0; $i < $size; $i++) {
$song_id[$i] ^= $magic[$i % count($magic)];
}
$result = base64_encode(md5($this->toStr($song_id), true));
$result = str_replace(['/', '+'], ['_', '-'], $result);
$sufix = $result . '/' . number_format($id, 0, '', '') . ".mp3";
switch ($type) {
case "hd":$url = self::mp3_hd_url . $sufix;break;
case "sd":$url = self::mp3_sd_url . $sufix;break;
default: $url = NULL; break;
}
return $url;
}
/**
* 登录操作
* @deprecated 登陆接口已作废
* @param String $username 用户名
* @param String $password 密码
* @return string 返回详细信息
*/
public function login($username, $password) {
$url = self::api_url . 'login';
$data = http_build_query(array(
'username' => $username,
'password' => md5($password),
'rememberLogin' => 'true',
));
$result = [];
$jsonArr = self::request($url, $data);
if ($jsonArr['code'] === 200) {
$result['status'] = 'success';
$result['info'] = array();
$result['info']['userId'] = $jsonArr['profile']['userId'];
$result['info']['nickname'] = $jsonArr['profile']['nickname'];
} else {
$result['status'] = 'false';
}
return json_encode($result, JSON_UNESCAPED_UNICODE);
}
/**
* 根据userId获得歌单
* @param int $userId 用户ID
* @return string 歌单
*/
public function get_playlist_by_user($userId) {
$url = self::api_url . 'user/playlist/?offset=0&limit=100&uid=' . $userId;
$result = [];
$jsonArr = self::request($url);
$playlist = $jsonArr['playlist'];
if ($playlist && $jsonArr['code'] === 200) {
$result['status'] = 'success';
$result['info'] = array();
$size = count($playlist);
for ($i = 0; $i < $size; $i++) {
$arrayName = array(
'name' => $playlist[$i]['name'],
'playlistId' => $playlist[$i]['id'],
);
$result['info'][] = $arrayName;
}
} else {
$result['status'] = 'false';
}
return json_encode($result, JSON_UNESCAPED_UNICODE);
}
/**
* 根据歌单ID获得详细信息
* @param int $playlistId 歌单id
* @return string details_list
*/
public function get_playlist_details($playlistId) {
$url = self::api_url . 'playlist/detail?id=' . $playlistId;
$result = [];
$jsonArr = self::request($url);
$songsList = $jsonArr['result']['tracks'];
if ($jsonArr['code'] === 200) {
$result['status'] = 'success';
$result['info'] = array();
$size = count($songsList);
for ($i = 0; $i < $size ; $i++) {
$song = self::get_song_by_id($songsList[$i]['id']);
if ($song['status'] === 'success') {
$result['info'][] = $song['info'];
}
}
} else {
$result['status'] = 'false';
}
return json_encode($result, JSON_UNESCAPED_UNICODE);
}
/**
* 根据歌手名获得详细信息
* @param String $name 歌手名
* @return string 详细信息
*/
public function search_artist_by_name($name) {
$url = self::api_url . 'search/get';
$data = http_build_query(array(
's' => $name,
'type' => 100,
'offset' => 0,
'sub' => 'false',
'limit' => 10,
));
$result = [];
$jsonArr = self::request($url, $data);
if ($jsonArr['code'] === 200) {
$result['status'] = 'success';
if ($jsonArr['result']['artistCount'] === 0) {
$result['info'] = '没查找到歌手';
} else {
$artistsList = $jsonArr['result']['artists'];
$result['info'] = array();
$size = count($artistsList);
for ($i = 0; $i < $size; $i++) {
$artist = array(
'artistId' => $artistsList[$i]['id'],
'artistName' => $artistsList[$i]['name'],
'artistImgUrl' => $artistsList[$i]['picUrl'],
);
$result['info'][] = $artist;
}
}
} else {
$result['status'] = 'false';
}
return json_encode($result, JSON_UNESCAPED_UNICODE);
}
/**
* 根据专辑名获得详细信息
* @param String $name 专辑名
* @return string 详细信息
*/
public function search_album_by_name($name) {
$url = self::api_url . 'search/get';
$data = http_build_query(array(
's' => $name,
'type' => 10,
'offset' => 0,
'sub' => 'false',
'limit' => 20,
));
$result = [];
$jsonArr = self::request($url, $data);
if ($jsonArr['code'] === 200) {
$result['status'] = 'success';
if ($jsonArr['result']['albumCount'] === 0) {
$result['info'] = '没查找到专辑';
} else {
$albumsList = $jsonArr['result']['albums'];
$result['info'] = array();
$size = count($albumsList);
for ($i = 0; $i < $size ; $i++) {
$album = array(
'albumId' => $albumsList[$i]['id'],
'albumName' => $albumsList[$i]['name'],
'albumImgUrl' => $albumsList[$i]['picUrl'],
);
$result['info'][] = $album;
}
}
} else {
$result['status'] = 'false';
}
return json_encode($result, JSON_UNESCAPED_UNICODE);
}
/**
* 根据歌曲名获得详细信息
* @param String $name 歌曲名
* @return string 详细信息
*/
public function search_song_by_name($name) {
$url = self::api_url . 'search/get';
$data = http_build_query(array(
's' => $name,
'type' => 1,
'offset' => 0,
'sub' => 'false',
'limit' => 100,
));
$result = [];
$jsonArr = self::request($url, $data);
if ($jsonArr['code'] === 200) {
$result['status'] = 'success';
if ($jsonArr['result']['songCount'] === 0) {
$result['info'] = '没查找到歌曲';
} else {
$songsList = $jsonArr['result']['songs'];
$result['info'] = array();
// 遍历歌曲
$size = count($songsList);
for ($i = 0; $i < $size; $i++) {
$song = self::get_song_by_id($songsList[$i]['id']);
if ($song['status'] === 'success') {
$result['info'][] = $song['info'];
}
}
}
} else {
$result['status'] = 'false';
}
return json_encode($result, JSON_UNESCAPED_UNICODE);
}
/**
* 根据歌手ID获得歌手专辑
* @param Int $artistId 歌手ID
* @return string 专辑信息
*/
public function get_artist_albums_by_id($artistId) {
$result = [];
$url = self::api_url . 'artist/albums/' . $artistId . '?offset=0&limit=50';
$jsonArr = self::request($url);
if ($jsonArr['code'] === 200) {
$result['status'] = 'success';
$albumsList = $jsonArr['hotAlbums'];
$result['info'] = array();
$size = count($albumsList);
for ($i = 0; $i < $size; $i++) {
$album = array(
'albumId' => $albumsList[$i]['id'],
'albumName' => $albumsList[$i]['name'],
'imgUrl' => $albumsList[$i]['picUrl'],
);
$result['info'][] = $album;
}
} else {
$result['status'] = 'false';
}
return json_encode($result, JSON_UNESCAPED_UNICODE);
}
/**
* 根据专辑ID取得专辑歌曲详细信息
* @param int $albumId 专辑ID
* @return string 详细信息
*/
public function get_album_songs($albumId) {
$result = [];
$url = self::api_url . 'album/' . $albumId . '/';
$jsonArr = self::request($url);
if ($jsonArr['code'] === 200) {
$result['status'] = 'success';
$songsList = $jsonArr['album']['songs'];
$result['info'] = array();
$size = count($songsList);
for ($i = 0; $i < $size; $i++) {
$song = self::get_song_by_id($songsList[$i]['id']);
if ($song['status'] === 'success') {
$result['info'][] = $song['info'];
}
}
} else {
$result['status'] = 'false';
}
return json_encode($result, JSON_UNESCAPED_UNICODE);
}
/**
* 根据歌曲ID获得详细信息
* @param Int $songId 歌曲ID
* @return string 信息数组
*/
public function get_song_by_id($songId) {
$result = [];
$url = self::api_url . 'song/detail?id=' . $songId . '&ids=[' . $songId . ']';
$jsonArr = self::request($url);
if ($jsonArr['code'] === 200) {
$result['status'] = 'success';
$songsList = $jsonArr['songs'];
$result['info'] = array();
$size = count($songsList);
for ($i = 0; $i < $size; $i++) {
$artistName = array();
$artistId = array();
// 遍历歌手
foreach ($songsList[$i]['artists'] as $artist) {
$artistName[] = $artist['name'];
$artistId[] = $artist['id'];
}
$song = array(
'songId' => $songsList[$i]['id'],
'songName' => $songsList[$i]['name'],
'albumName' => $songsList[$i]['album']['name'],
'albumId' => $songsList[$i]['album']['id'],
'artistId' => $artistId,
'artistName' => $artistName,
'mp3Url' => self::get_hd_mp3_url(number_format($songsList[$i]['hMusic']['dfsId'], 0, '', '')),
'imgUrl' => $songsList[$i]['album']['picUrl']);
$result['info'][] = $song;
}
} else {
$result['status'] = 'false';
}
return $result;
}
}