[发布&demo]
PHP向 一个简易的虾米,网易云音乐接口解析类
1 #
发表于 2015-4-28 07:28:09
|
查看: 7381
|
回复: 13
最近在写论坛的音区和电台功能 用到了虾米和网易的资源
在这里分享一下自己整合的一个功能简易的类 唯一的效果只是访问对方的API接口然后返回歌曲的信息使用方法很简单
$xiami = GetSong::xiami(1774092474) 返回一个虾米id为1774092474的歌曲数据数组
返回的数据有(网易同理)
=>['url'] MP3地址
=>['title'] 歌曲标题
=>['album_name'] 专辑标题
=>['artist_name'] 艺术家名字
=>['album_id'] 专辑ID
=>['pic'] 封面图片地址
=>['type'] 网站类型 (虾米是xiami 网易是wymusic)
针对虾米的封面地址 返回的默认是最小尺寸的 如果想要大尺寸的封面的话要在数据返回以后修改一下字符串
例如 $xiami['pic'] = str_replace('_1','',$xiami['pic']); 删除字符串中的"_1"
由于实在是非常简易就不多做解说了下面是代码
<?php
class GetSong{
public static function xiami($id){
$data = array();
$content = self::__curl('http://www.xiami.com/song/playlist/id/'.$id.'/object_name/default/object_id/0/cat/json', $_SERVER['HTTP_HOST']);
$json = json_decode($content,true);
$song = $json['data']['trackList'][0];
$data['url'] = self::__getXiamiMp3($song['location']);
$data['title'] = $song['title'];
$data['album_name'] = $song['album_name'];
$data['artist_name'] = $song['artist'];
$data['album_id'] = $song['album_id'];
$data['pic'] = $song['pic'];
$data['type'] = 'xiami';
if(!empty($data['title'])){
return $data;
}else{
return false;
}
}
public static function wymusic($id){
$url = 'http://music.163.com/api/song/detail/?id='.$id.'&ids=['.$id.']';
$content = self::__curl($url,'http://music.163.com/');
$json = json_decode($content,true);
$song = $json['songs'][0];
$album = $song['album'];
$data = array();
$data['url'] = $song['mp3Url'];
$data['title'] = $song['name'];
$data['album_id'] = $album['id'];
$data['album_name'] = $album['name'];
$data['artist_name'] = $song['artists'][0]['name'];
$data['pic'] = $album['picUrl'];
$data['type'] = '163music';
if(!empty($data['title'])){
return $data;
}else{
return false;
}
}
private static function __getXiamiMp3($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;
}
private static function __curl($url,$referer){
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt ($curl, CURLOPT_REFERER, $url);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7");
$src = curl_exec($curl);
curl_close($curl);
return $src;
}
}
?> 复制代码