[作者:张赐荣]
获取喜马拉雅音频直链的方法
对以下地址发起Request Get请求,返回Json格式结果,其中的data.src就是音频的真实路径。
"https://www.ximalaya.com/revision/play/v1/audio?ptype=1&id=soundID"
参数为sound id。
例如以下音频地址:"http://www.ximalaya.com/43755913/sound/355188898"
取"/sound/"候面的一串数字,得到音频ID"355188898"
形成请求地址:"https://www.ximalaya.com/revision/play/v1/audio?ptype=1&id=355188898"
返回JSON
{"ret":200,"data":{"trackId":355188898,"canPlay":true,"isPaid":false,"hasBuy":true,"src":"https://aod.cos.tx.xmcdn.com/storages/b35f-audiofreehighqps/3B/2A/CMCoOSIDhXNVARUyWwBm3Gnh.m4a","albumIsSample":false,"sampleDuration":180,"isBaiduMusic":false,"firstPlayStatus":true,"isVipFree":false,"isXimiAhead":false,"isAlbumTimeLimited":false,"ximiVipFreeType":0,"joinXimi":false}}
以下PHP代码封装了获取喜马拉雅音频直链地址的函数,传入音频ID(URL sound 之后的那串数字)即可
<?php
echo(GetLink("355188898"));
function GetLink ($sid)
{
$gc_url="https://www.ximalaya.com/revision/play/v1/audio?ptype=1&id=".$sid;
$gc_ch = curl_init();
curl_setopt($gc_ch, CURLOPT_L_VERIFYPEER, FALSE);
curl_setopt($gc_ch, CURLOPT_L_VERIFYHOST, FALSE);
curl_setopt($gc_ch, CURLOPT_REFERER,$gc_url);
curl_setopt($gc_ch, CURLOPT_URL,$gc_url);
curl_setopt($gc_ch, CURLOPT_USERAGENT,'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.81');
curl_setopt($gc_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($gc_ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($gc_ch, CURLOPT_HEADER,0);
$gc_content = curl_exec($gc_ch);
curl_close($gc_ch);
$resultObject=json_decode($gc_content);
return($resultObject->data->src);
}
?>