歌名搜索,获取百度MP3信息,
自己写的注释比较全,
有兴趣的朋友可以玩玩,自己搞个mp3下载站
更多技术文章请移步
http://www.dahuzhi.com
<?php
header("Content-Type:text/html; charset=utf-8");
set_time_limit(0);//设置PHP超时时间
$mp3 = new GetBaiduMp3();
$data = $mp3->GetMp3('老男孩');
var_dump($data);
class GetBaiduMp3
{
/**
* 搜索百度mp3
* 作者: zhanya4548 email: 23585472@qq.com
* @param $key 歌曲名
* @return array 返回所有内容列表
'song_id' => 歌曲百度id
'song' => 歌曲名称
'singer' => 歌手
'album' => 专辑
'singerPicSmall' => 歌曲小图
'singerPicLarge' => 歌曲大图
'albumPicSmall' => 专辑小图
'albumPicLarge' => 专辑大图
'lrc' => 歌词
'mp3Link' => mp3地址
*/
public function GetMp3($key)
{
/* 搜索歌曲
参数:
word: "歌曲名", //歌曲名 encodeURI
format: "json", //返回数据格式,xml | json,默认xml
callback: "Pub.music.searchResult", //固定值,返回jsonp格式
返回:
'song' => string '老男孩' (length=9)
'song_id' => string '5830696' (length=7)
'singer' => string '筷子兄弟' (length=12)
'album' => string '老男孩' (length=9)
'singerPicSmall' => string 'http://qukufile2.qianqian.com/data2/pic/86604899/86604899.jpg' (length=61)
'singerPicLarge' => string 'http://qukufile2.qianqian.com/data2/pic/48691336/48691336.jpg' (length=61)
'albumPicLarge' => string 'http://qukufile2.qianqian.com/data2/pic/38684666/38684666.jpg' (length=61)
'albumPicSmall' => string 'http://qukufile2.qianqian.com/data2/pic/38684670/38684670.jpg' (length=61)
*/
$url = "http://mp3.baidu.com/dev/api/?tn=getinfo&ct=0&word=" . $key . "&ie=utf-8&format=json";
$data = $this->GetCon($url);
$mp3_info = json_decode($data, true);
/* 获取指定歌曲
参数:
songIds: "38233821" //歌曲id,从第1个列表中得到的歌曲id
callback: "callback" //不为空时,返回jsonp格式数据
返回:
array
'errorCode' => int 22000
'data' =>
array
'xcode' => string '521d9a8c84a07842553396c56deab3f1' (length=32)
'songList' =>
array
0 =>
array
'queryId' => string '5830696' (length=7)
'songId' => int 5830696
'songName' => string '老男孩' (length=9)
'artistId' => string '9295' (length=4)
'artistName' => string '筷子兄弟' (length=12)
'albumId' => int 5830697
'albumName' => string '老男孩' (length=9)
'songPicSmall' => string 'http://a.hiphotos.baidu.com/ting/pic/item/3b87e950352ac65c434a5feefaf2b21192138adb.jpg' (length=86)
'songPicBig' => string 'http://c.hiphotos.baidu.com/ting/pic/item/b7003af33a87e9506458caf111385343faf2b4db.jpg' (length=86)
'songPicRadio' => string 'http://a.hiphotos.baidu.com/ting/pic/item/37d3d539b6003af375f5e5fd342ac65c1138b6db.jpg' (length=86)
'lrcLink' => string '/data2/lrc/14883173/14883173.lrc' (length=32)
'version' => string '' (length=0)
'copyType' => int 1
'time' => int 300
'linkCode' => int 22000
'songLink' => string 'http://zhangmenshiting.baidu.com/data2/music/42657284/5830696230400.mp3?xcode=521d9a8c84a0784260483b33ab4fa0d0cb094c8a407e2e06' (length=126)
'showLink' => string 'http://zhangmenshiting.baidu.com/data2/music/42657284/5830696230400.mp3?xcode=521d9a8c84a0784260483b33ab4fa0d0cb094c8a407e2e06' (length=126)
'format' => string 'mp3' (length=3)
'rate' => int 128
'size' => int 4801083
'relateStatus' => string '0' (length=1)
'resourceType' => string '0' (length=1)
*/
$i = 0;
$mp3_info3 = array();
foreach ($mp3_info as $v)
{
$mp3_info3[$i]['song_id'] = $v['song_id']; //歌曲id
$mp3_info3[$i]['song'] = $v['song']; //歌名
$mp3_info3[$i]['singer'] = $v['singer']; //歌手
$mp3_info3[$i]['album'] = $v['album']; //专辑
$mp3_info3[$i]['singerPicSmall'] = $v['singerPicSmall']; //歌手图片小
$mp3_info3[$i]['singerPicLarge'] = $v['singerPicLarge']; //歌手图片大
$mp3_info3[$i]['albumPicSmall'] = $v['albumPicSmall']; //专辑图片小
$mp3_info3[$i]['albumPicLarge'] = $v['albumPicLarge']; //专辑图片大
$url2 = "http://ting.baidu.com/data/music/links?songIds=" . $v['song_id'];
$data2 = $this->GetCon($url2);
$mp3_info2 = json_decode($data2, true);
//歌词下载 http://ting.baidu.com/data2/lrc/15265710/15265710.lrc
$mp3_info3[$i]['lrc'] = 'http://ting.baidu.com/' . $mp3_info2['data']['songList'][0]['lrcLink'];
//mp3下载地址 http://zhangmenshiting.baidu.com/data2/music/38542270/382338211365422461.mp3?xcode=808f67065a7ea25b17e77954bed13215
$mp3_info3[$i]['mp3Link'] = $mp3_info2['data']['songList'][0]['songLink'];
$i++;
}
return $mp3_info3;
}
/**
* 访问网址并取得其内容
* @param $url String 网址
* @param $postFields Array 将该数组中的内容用POST方式传递给网址中
* @param $cookie_file string cookie文件
* @param $r_or_w string 写cookie还是读cookie或是两都都有,r读,w写,a两者,null没有cookie
* @return String 返回网址内容
*/
public function GetCon($url, $postFields = null, $cookie_file = null, $r_or_w = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
if ($cookie_file && ($r_or_w == 'a' || $r_or_w == 'w'))
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); // 存放Cookie信息的文件名称
if ($cookie_file && ($r_or_w == 'a' || $r_or_w == 'r'))
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); // 读取上面所储存的Cookie信息
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (is_array($postFields) && 0 < count($postFields))
{
$postBodyString = "";
foreach ($postFields as $k => $v)
{
$postBodyString .= "$k=" . urlencode($v) . "&";
}
unset($k, $v);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
}
$reponse = curl_exec($ch);
if (curl_errno($ch))
throw new Exception(curl_error($ch),0);
else
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $reponse;
}
}