- 2016 03/23
本文转自:http://yuanmu.mzzhost.com/
1、虾米
项目地址:https://github.com/xyuanmu/parsexiami
测试地址:http://yuanmu.mzzhost.com/xiami/
另:标准音质下载
<?php
//方法来自网络
ERROR_REPORTING(0);
$id= '';
$content=file_get_contents('http://www.xiami.com/widget/xml-single/sid/'.$id); //获取组合地址内容
preg_match("/<\!\[CDATA\[([^\]].*)\]\]><\/location>/i",$content,$location);//匹配location内容
$xiami=ipcxiami($location[1]); //转码得到正确的地址
header('Content-Type:application/force-download'); //强制下载
header("Location:".$xiami);
function ipcxiami($location){
$count = (int)substr($location, 0, 1);
$url = substr($location, 1);
$line = floor(strlen($url) / $count);
$loc_5 = strlen($url) % $count;
$loc_6 = array();
$loc_7 = 0;
$loc_8 = '';
$loc_9 = '';
$loc_10 = '';
while ($loc_7 < $loc_5){
$loc_6[$loc_7] = substr($url, ($line+1)*$loc_7, $line+1);
$loc_7++;
}
$loc_7 = $loc_5;
while($loc_7 < $count){
$loc_6[$loc_7] = substr($url, $line * ($loc_7 - $loc_5) + ($line + 1) * $loc_5, $line);
$loc_7++;
}
$loc_7 = 0;
while ($loc_7 < strlen($loc_6[0])){
$loc_10 = 0;
while ($loc_10 < count($loc_6)){
$loc_8 .= @$loc_6[$loc_10][$loc_7];
$loc_10++;
}
$loc_7++;
}
$loc_9 = str_replace('^', 0, urldecode($loc_8));
return $loc_9;
}
?>
2、网易云音乐
class Get_Netease{
public function netease_song($music_id){
$key = "/netease/song/$music_id";
$cache = $this->get_cache($key);
#if( $cache ) return $cache;
$url = "http://music.163.com/api/song/detail/?id=" . $music_id . "&ids=%5B" . $music_id . "%5D";
$response = $this->netease_http($url);
if( $response["code"]==200 && $response["songs"] ){
//处理音乐信息
$songs = $response["songs"][0];
$mp3_url = $songs["mp3Url"];
if (in_array("hMusic", $songs)){
$id = number_format($songs["hMusic"]["dfsId"], 0,'','');
$songUrl = explode("/", $mp3_url);
$mp3_url = "http://" . $songUrl[2] . "/" . $this->encrypted_id($id) . "/" . $id . ".mp3";
}
$music_name = $songs["name"];
$artists = array();
foreach ($songs["artists"] as $artist) {
$artists[] = $artist["name"];
}
$artists = implode(",", $artists);
$result = array(
"song_id" => $music_id,
"song_title" => $music_name,
"song_author" => $artists,
"song_src" => $mp3_url
);
$this->set_cache($key, $result, 24);
return $result;
}
return false;
}
private function netease_http($url){
$refer = "http://music.163.com/";
$header[] = "Cookie: appver=2.0.2;";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $refer);
$cexecute = curl_exec($ch);
curl_close($ch);
if ($cexecute) {
$result = json_decode($cexecute, true);
return $result;
}else{
return false;
}
}
private function encrypted_id($dfsid){
$key = '3go8&$8*3*3h0k(2)2';
$key_len = strlen($key);
for($i = 0; $i < strlen($dfsid); $i++){
$dfsid[$i] = $dfsid[$i] ^ $key[$i % $key_len];
}
$raw_code = base64_encode(md5($dfsid, true));
$code = str_replace(array('/', '+'), array('_', '-'), $raw_code);
return $code;
}
public function get_cache($key){
$cache = get_transient($key);
return $cache === false ? false : json_decode($cache, true);
}
public function set_cache($key, $value, $hour=1){
$value = json_encode($value);
set_transient($key, $value, 60*60*$hour);
}
}
$get_netease = new Get_Netease;
function get_music($url){
$music = '';
if (preg_match('#http://music.163.com/\#/song\?id=(\d+)#i', $url, $matches)) {
global $get_netease;
$song_id = $matches[1];
$netease_json = $get_netease->netease_song($song_id);
$music = $netease_json['song_src'];
}
return $music;
}
echo get_music("http://music.163.com/#/song?id=190449");
“首先声明下,部分源码来自WP插件Hermit,加上从python解析插件you-get里的计算encrypted_id的代码,由本人亲自重新编译成PHP,两者一组合就得到了这份完美的解析高品质网易云音乐的PHP代码。因为部分所需函数,所以除了PHP环境外还需要安装WordPress。”

