可笑的是一个技术小白离开了百度啥也不会写,全程靠百度。
<?php
/**
* @author Youngxj <blog@youngxj.cn>
* @time 2018年6月22日
* @todo 利用酷狗歌单进行获取酷狗歌曲信息
*/
// 访问短网址出现的地址
// http://m.kugou.com/share/zlist.html?u=25230245&h1=142560927507380788791872687420293028022&h2=da7ce5776afd5fdba03daa05f4a19860&listid=25&uid=25230245&type=0&_t=1528116907&token=2b631b0f122f5dc5c0ca758374c4d190eb8bde2ea382bf7a29bacfd5bb439489&sign=c0c7fe2caee03445b51bf8961308174b&chl=qq_client
// 歌单列表接口
// http://m.kugou.com/zlist/list?listid=25&type=0&uid=25230245&sign=c0c7fe2caee03445b51bf8961308174b&_t=1528116907&pagesize=30&json=&page=1&token=2b631b0f122f5dc5c0ca758374c4d190eb8bde2ea382bf7a29bacfd5bb439489
// 关键值 uid sign _t token listid type
// 获取歌曲信息的接口
// http://m.kugou.com/app/i/getSongInfo.php?hash=348CE533CE66E6DCEDA4A9FD63AB6D86&cmd=playInfo&is_share=1
# 设置json格式
header('Content-type: application/json');
#缓存文件名(最好使用json文件格式)
$fileName = 'data.json';
# 酷狗歌单分享出来的短网址
$kgUrl = "http://t.kugou.com/1md5hf5t8V2";
file_get_contents($kgUrl);
# 获取歌单访问后302跳转的header头信息
$responseInfo = $http_response_header;
# 第五条记录为Location跳转地址里面包含多个关键值
$local = $responseInfo['5'];
# 下面是蹩脚正则取值
// uid(疑似用户id)
$zz_uid = "/uid=(.*?)&/";
preg_match($zz_uid,$local,$uid);
// sign
$zz_sign = "/sign=(.*?)&/";
preg_match($zz_sign,$local,$sign);
// _t
$zz_t = "/_t=(.*?)&/";
preg_match($zz_t,$local,$t);
// listid(疑似歌单id)
$zz_listid = "/listid=(.*?)&/";
preg_match($zz_listid,$local,$listid);
// type
$zz_type = "/type=(.*?)&/";
preg_match($zz_type,$local,$type);
// token
$zz_token = "/token=(.*?)&/";
preg_match($zz_token,$local,$token);
//echo '<br/>';
# 把取到的值变量定义
$uid = $uid['1'];
$sign = $sign['1'];
$t = $t['1'];
$listid = $listid['1'];
$type = $type['1'];
$token = $token['1'];
# 这里是呼应后面文本缓存的
# 主要是判断文本缓存中的token是否与当前获取的token是否一致
# 如果一致则读取文本缓存的json
# 如果不一致则使用后面的获取最新
# 继续写入文本缓存
if(is_file($fileName)){
$dataJson = file_get_contents($fileName);
$dataJsons = json_decode($dataJson,1);
if($token == $dataJsons['token']){
echo $dataJson;
exit();
}
}
# 这个地址是获取歌单列表里有多少歌
$listUrl = 'http://m.kugou.com/zlist/list?listid='.$listid.'&type='.$type.'&uid='.$uid.'&sign='.$sign.'&_t='.$t.'&pagesize=30&json=&page=1&token='.$token;
$list = file_get_contents($listUrl);
//var_dump($list);
$lists = json_decode($list,1);
# 统计歌单歌曲总数
$coun = count($lists["list"]["info"]);
$arr = array('token'=>$token);
for ($i=0; $i < $coun; $i++) {
# 利用前面歌单列表取到的hash
# 进行二次临时歌曲地址寻找
$hash = $lists["list"]["info"][$i]['hash'];
$getSongInfoUrl = 'http://m.kugou.com/app/i/getSongInfo.php?hash='.$hash.'&cmd=playInfo&is_share=1';
$get = file_get_contents($getSongInfoUrl);
$getjson = json_decode($get,1);
$arr[$i] = $getjson;
}
# 到这里已经能正常输出歌单里所有歌曲的信息
echo $data_json = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
# 后面的为文本进行缓存的技术
$myfile = fopen($fileName, "w") or die("无法打开文件,请确定是否有权限编辑文件");
$txt = $data_json;
fwrite($myfile, $txt);
fclose($myfile);
如果有什么错误,请指出来,技术小白不易。
本文参考《网页抓包之我的歌单》进行长达一个小时进行编写调试。













发表评论: