{ "code": 200, "msg": "请求成功", "data": { "song_name": "泡沫", "time": 1511086810, "pic": "http://y.gtimg.cn/music/photo_new/T002R500x500M000002YFufr4bXZyI.jpg?max_age=2592000", "musiclink": "http://tx.stream.kg.qq.com/gzc-kgsvp/gzc_0_50111_1021_9df7bcefa1f1ebab547e47ef80f9cefd768137ae.f0.m4a?vkey=D799F0F7A92E4D64CE7F64CA9FEAE2E5C4EBB6079E4ACA36F12E73D4CBCFB1950854101FFDEBEF3A0BE884A7F55743CAD64CE541A48D5267385DBFE62D254368A35351DA1391EACF919B97698B1E55DD16AA2F8D7AE92EC0&dis_k=6ac4c412b515a663e559571cad9cc399&dis_t=1660377897&fromtag=1021&sdtfrom=v1021&ugcid=283962548_1511086810_232", "author": { "name": "http://shp.qlogo.cn/ttsing/283962548/283962548/100", "avatar": "19A-张承惠" } } }
<?php /** * 全民K歌直链解析 * @author 晓超云博客 * @link https://blog.kit9.cn/ * @time 2022年08月13日 */ header("content-type:application/json; charset=utf-8"); $url = $_GET['url']; if (empty($url)) { echo json_encode(['code' => 1, 'msg' => '分享链接不能为空,例如https://kg.qq.com/node/play?s=riEBRhrlX8YxXrUT&g_f=share_html'], 320); exit; }elseif (strpos($url,'kg.qq.com') == false) { echo json_encode(['code' => 0, 'msg' => '您输入的链接,不是全民K歌分享链接'], 320); exit; } $data = curl($url); preg_match("/window.__DATA__ =(.*?);/",$data,$json); $arr=json_decode($json[1],true); $value = [ 'code' => 200, 'msg' => '请求成功', 'data' => [ 'song_name' => $arr['detail']['song_name'],//歌曲名 'time' => $arr['detail']['ctime'],//发布时间 'pic' => $arr['detail']['cover'],//封面 'musiclink' => $arr['detail']['playurl'],//音乐直链 'author' => [ 'name' => $arr['detail']['avatar'],//歌手昵称 'avatar' => $arr['detail']['kg_nick']//歌手头像 ]]]; echo json_encode($value,460); function curl($url){ //Curl GET $ch = curl_init(); // Curl 初始化 $timeout = 30; // 超时时间:30s $ua='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';// 伪造抓取 UA $ip = mt_rand(11, 191) . "." . mt_rand(0, 240) . "." . mt_rand(1, 240) . "." . mt_rand(1, 240); curl_setopt($ch, CURLOPT_URL, $url);// 设置 Curl 目标 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// Curl 请求有返回的值 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);// 设置抓取超时时间 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// 跟踪重定向 curl_setopt($ch, CURLOPT_REFERER, 'https://www.baidu.com/');//模拟来路 curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip)); //伪造IP curl_setopt($ch, CURLOPT_USERAGENT, $ua);// 伪造ua curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// https请求 不验证证书和hosts curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//强制协议为1.0 curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );//强制使用IPV4协议解析域名 $content = curl_exec($ch); curl_close($ch);// 结束 Curl return $content;// 函数返回内容 }