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
| <?php
function music_list_id() { $id = '2493852196'; $data = file_get_contents('http://music.163.com/api/playlist/detail?id=' . $id); $json = json_decode($data, true); $count = count($json['result']['tracks']); $random = mt_rand(0, $count ); return $json['result']['tracks'][$random]['id']; }
function curl_get($url, $referurl = '') { if (empty($referurl)) { $referurl = $url; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_REFERER, $referurl); $output = curl_exec($ch); curl_close($ch); return $output; }
function song_url() { $id = music_list_id(); $api = 'http://music.163.com/song/media/outer/url?id='; $url = $api . $id . '.mp3'; $data = curl_get($url); preg_match_all('/Location:(.*?)\.mp3/', $data, $arr); if (!empty($arr[1][0])) { $play = $arr[1][0]; return str_replace('http://', 'https://', trim($play) . '.mp3'); } }
function music_hot_comments() { $id = music_list_id(); $data = file_get_contents('http://music.163.com/api/v1/resource/comments/R_SO_4_' . $id); $json = json_decode($data, true); if ($json['total'] > 0) { $arr= [ 'id' => $id, 'user' => $json['hotComments'][0]['user']['nickname'], 'pic' => $json['hotComments'][0]['user']['avatarUrl'], 'content' => $json['hotComments'][0]['content'], 'song_url' => song_url() ]; return $arr; } } echo music_hot_comments();
|