-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNeteaseMusicAPI_mini.php
More file actions
216 lines (198 loc) · 7.47 KB
/
NeteaseMusicAPI_mini.php
File metadata and controls
216 lines (198 loc) · 7.47 KB
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
<?php
/*!
* Netease Cloud Music Api - mini
* https://i-meto.com
* Version 3.0.0
*
* Copyright 2016, METO
* Released under the MIT license
*/
class NeteaseMusicAPI{
// General
protected $_MINI_MODE=false;
protected $_MODULUS='00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7';
protected $_NONCE='0CoJUm6Qyw8W8jud';
protected $_PUBKEY='010001';
protected $_VI='0102030405060708';
protected $_USERAGENT='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.157 Safari/537.36';
protected $_COOKIE='os=pc; osver=Microsoft-Windows-10-Professional-build-10586-64bit; appver=2.0.3.131777; channel=netease; __remember_me=true';
protected $_REFERER='http://music.163.com/';
// use static secretKey, without RSA algorithm
protected $_secretKey='TA3YiYCfY2dDJQgg';
protected $_encSecKey='84ca47bca10bad09a6b04c5c927ef077d9b9f1e37098aa3eac6ea70eb59df0aa28b691b7e75e4f1f9831754919ea784c8f74fbfadf2898b0be17849fd656060162857830e241aba44991601f137624094c114ea8d17bce815b0cd4e5b8e2fbaba978c6d1d14dc3d1faf852bdd28818031ccdaaa13a6018e1024e2aae98844210';
// encrypt mod
protected function prepare($raw){
$data['params']=$this->aes_encode(json_encode($raw),$this->_NONCE);
$data['params']=$this->aes_encode($data['params'],$this->_secretKey);
$data['encSecKey']=$this->_encSecKey;
return $data;
}
protected function aes_encode($secretData,$secret){
return openssl_encrypt($secretData,'aes-128-cbc',$secret,false,$this->_VI);
}
// CURL
protected function curl($url,$data=null){
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
if($data){
if(is_array($data))$data=http_build_query($data);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl,CURLOPT_POST,1);
}
curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl,CURLOPT_REFERER,$this->_REFERER);
curl_setopt($curl,CURLOPT_COOKIE,$this->_COOKIE);
curl_setopt($curl,CURLOPT_USERAGENT,$this->_USERAGENT);
$result=curl_exec($curl);
curl_close($curl);
return $result;
}
// main function
public function search($s,$limit=30,$offset=0,$type=1){
$url='http://music.163.com/weapi/cloudsearch/get/web?csrf_token=';
$data=array(
's'=>$s,
'type'=>$type,
'limit'=>$limit,
'total'=>'true',
'offset'=>$offset,
'csrf_token'=>'',
);
$raw=$this->curl($url,$this->prepare($data));
if($this->_MINI_MODE){
$this->_MINI_MODE=false;
$raw=json_decode($raw,1);
return json_encode($this->clear_data($raw["result"]["songs"]));
}
else return $raw;
}
public function artist($artist_id){
$url='http://music.163.com/weapi/v1/artist/'.$artist_id.'?csrf_token=';
$data=array(
'csrf_token'=>'',
);
$raw=$this->curl($url,$this->prepare($data));
if($this->_MINI_MODE){
$this->_MINI_MODE=false;
$raw=json_decode($raw,1);
return json_encode($this->clear_data($raw["hotSongs"]));
}
else return $raw;
}
public function album($album_id){
$url='http://music.163.com/weapi/v1/album/'.$album_id.'?csrf_token=';
$data=array(
'csrf_token'=>'',
);
$raw=$this->curl($url,$this->prepare($data));
if($this->_MINI_MODE){
$this->_MINI_MODE=false;
$raw=json_decode($raw,1);
return json_encode($this->clear_data($raw["songs"]));
}
else return $raw;
}
public function detail($song_id){
$url='http://music.163.com/weapi/v3/song/detail?csrf_token=';
$data=array(
'c'=>'['.json_encode(array('id'=>$song_id)).']',
'csrf_token'=>'',
);
$raw=$this->curl($url,$this->prepare($data));
if($this->_MINI_MODE){
$this->_MINI_MODE=false;
$raw=json_decode($raw,1);
return json_encode($this->clear_data($raw["songs"]));
}
else return $raw;
}
public function url($song_id,$br=999000){
$url='http://music.163.com/weapi/song/enhance/player/url?csrf_token=';
if(!is_array($song_id))$song_id=array($song_id);
$data=array(
'ids'=>$song_id,
'br'=>$br,
'csrf_token'=>'',
);
return $this->curl($url,$this->prepare($data));
}
public function playlist($playlist_id){
$url='http://music.163.com/weapi/v3/playlist/detail?csrf_token=';
$data=array(
'id'=>$playlist_id,
'n'=>1000,
'csrf_token'=>'',
);
$raw=$this->curl($url,$this->prepare($data));
if($this->_MINI_MODE){
$this->_MINI_MODE=false;
$raw=json_decode($raw,1);
return json_encode($this->clear_data($raw["playlist"]["tracks"]));
}
else return $raw;
}
public function lyric($song_id){
$url='http://music.163.com/weapi/song/lyric?csrf_token=';
$data=array(
'id'=>$song_id,
'os'=>'pc',
'lv'=>-1,
'kv'=>-1,
'tv'=>-1,
'csrf_token'=>'',
);
return $this->curl($url,$this->prepare($data));
}
public function mv($mv_id){
$url='http://music.163.com/weapi/mv/detail?csrf_token=';
$data=array(
'id'=>$mv_id,
'csrf_token'=>'',
);
return $this->curl($url,$this->prepare($data));
}
protected function clear_data($result){
// you can modify it by yourself, change to your API?!
foreach($result as $key=>$vo){
$data[$key]=array(
'id'=>$key,
'songid'=>$vo["id"],
'name'=>$vo["name"],
'cover'=>'https://p4.music.126.net/'.self::Id2Url($vo['al']["pic_str"]).'/'.$vo['al']["pic_str"].'.jpg',
'url'=>'http://music.163.com/song/media/outer/url?id='.$vo["id"],
//'lyric'=>$vo["id"],
'artist'=>array(),
);
foreach($vo['ar'] as $vvo)$data[$key]['artist'][]=$vvo['name'];
$data[$key]['artist']=implode('/',$data[$key]['artist']);
}
return $data;
}
public function mini(){
$this->_MINI_MODE=true;
return $this;
}
/* static url encrypt, use for pic*/
public function Id2Url($id){
$byte1[]=$this->Str2Arr('3go8&$8*3*3h0k(2)2');
$byte2[]=$this->Str2Arr($id);
$magic=$byte1[0];
$song_id=$byte2[0];
for($i=0;$i<count($song_id);$i++)$song_id[$i]=$song_id[$i]^$magic[$i%count($magic)];
$result=base64_encode(md5($this->Arr2Str($song_id),1));
$result=str_replace('/','_',$result);
$result=str_replace('+','-',$result);
return $result;
}
protected function Str2Arr($string){
$bytes=array();
for($i=0;$i<strlen($string);$i++)$bytes[]=ord($string[$i]);
return $bytes;
}
protected function Arr2Str($bytes){
$str='';
for($i=0;$i<count($bytes);$i++)$str.=chr($bytes[$i]);
return $str;
}
}