前几日突发奇想,想做一个音乐的页面在博客中,然后想办法扒网易的api,然后把自己创建的歌单搬到博客中,demo:关关的音乐,nob大雄喜欢的音乐。
稳定运行了几日后,发布一下代码,网易云音乐的api整理出一个php类,有兴趣的可以下载使用。目前只有获取歌单信息、获取歌词,以后有时间继续完善。
网易云音乐的前后端交互使用的json,json是轻量级的数据交换格式,通常客户端用于解析数据并展示。
首先,获取歌单信息,每个歌单都有一个ID,在网页端查看的时候,url中
http://music.163.com/#/playlist?id=21264283
id=后面的一串数字就是歌单的ID
获取歌单信息的api接口就是:
public function getPlaylist($id){
$return = file_get_contents('http://music.163.com/api/playlist/detail?id='.$id);
return json_decode($return,true);
}
返回的json直接输出,得到:
{
"result": {
"subscribers": [],
"subscribed": false,
"creator": {
"signature": "",
"authority": 0,
"defaultAvatar": false,
"avatarImgId": 1368891984853723,
"province": 310000,
"authStatus": 0,
"followed": false,
"avatarUrl": "http://p3.music.126.net/8jyh9TDlzAIB-k8RQ77hng==/1368891984853723.jpg?imageView2/2/w/1280",
"accountStatus": 0,
"gender": 1,
"city": 310101,
"birthday": 813081600000,
"userId": 76521820,
"userType": 0,
"nickname": "关关Sinkey",
"description": "",
"detailDescription": "",
"backgroundImgId": 3440371893071195,
"backgroundUrl": "http://p1.music.126.net/YZUQN0n_ygaC2D_G2E25Vw==/3440371893071195.jpg?imageView2/2/w/1280",
"mutual": false,
"expertTags": null,
"djStatus": 0,
"vipType": 10,
"remarkName": null
},
"artists": null,
"tracks": [
{
"name": "痛快的哀艳",
"id": 35416420,
"position": 1,
"alias": [
"Violently Sad and Beautiful"
],
"status": 0,
"fee": 8,
"copyrightId": 11001,
"disc": "1",
"no": 1,
"artists": [
{
"name": "苏打绿",
"id": 12707,
"picId": 0,
"img1v1Id": 18686200114669624,
"briefDesc": "",
"picUrl": "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg?imageView2/2/w/1280",
"img1v1Url": "http://p4.music.126.net/VnZiScyynLG7atLIZ2YPkw==/18686200114669622.jpg?imageView2/2/w/1280",
"albumSize": 0,
"alias": [],
"trans": "",
"musicSize": 0,
"img1v1Id_str": "18686200114669622"
}
],
"album": {
"name": "冬 未了",
"id": 3319559,
"type": "专辑",
"size": 22,
"picId": 1401877340850803,
"blurPicUrl": "http://p3.music.126.net/b-TEytnobA_J4Z7m05tfPw==/1401877340850803.jpg?imageView2/2/w/1280",
"companyId": 0,
"pic": 1401877340850803,
"picUrl": "http://p3.music.126.net/b-TEytnobA_J4Z7m05tfPw==/1401877340850803.jpg?imageView2/2/w/1280",
"publishTime": 1446566400007,
"description": "",
"tags": "",
"company": "林暐哲音乐社",
"briefDesc": "",
"artist": {
"name": "",
"id": 0,
"picId": 0,
"img1v1Id": 18686200114669624,
"briefDesc": "",
"picUrl": "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg?imageView2/2/w/1280",
"img1v1Url": "http://p3.music.126.net/VnZiScyynLG7atLIZ2YPkw==/18686200114669622.jpg?imageView2/2/w/1280",
"albumSize": 0,
"alias": [],
"trans": "",
"musicSize": 0,
"img1v1Id_str": "18686200114669622"
},
"songs": null,
"alias": [],
"status": 3,
"copyrightId": 11001,
"commentThreadId": "R_AL_3_3319559",
"artists": [
{
"name": "苏打绿",
"id": 12707,
"picId": 0,
"img1v1Id": 18686200114669624,
"briefDesc": "",
"picUrl": "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg?imageView2/2/w/1280",
"img1v1Url": "http://p3.music.126.net/VnZiScyynLG7atLIZ2YPkw==/18686200114669622.jpg?imageView2/2/w/1280",
"albumSize": 0,
"alias": [],
"trans": "",
"musicSize": 0,
"img1v1Id_str": "18686200114669622"
}
]
},
"starred": false,
"popularity": 100,
"score": 100,
"starredNum": 0,
"duration": 360640,
"playedNum": 0,
"dayPlays": 0,
"hearTime": 0,
"ringtone": null,
"crbt": null,
"audition": null,
"copyFrom": "",
"commentThreadId": "R_SO_4_35416420",
"rtUrl": null,
"ftype": 0,
"rtUrls": null,
"hMusic": {
"name": null,
"id": 1233393307,
"size": 14427995,
"extension": "mp3",
"sr": 44100,
"dfsId": 18600438207368468,
"bitrate": 320000,
"playTime": 360640,
"volumeDelta": -2.38,
"dfsId_str": "18600438207368468"
},
"mMusic": {
"name": null,
"id": 1233393308,
"size": 7214020,
"extension": "mp3",
"sr": 44100,
"dfsId": 3311729028547741,
"bitrate": 160000,
"playTime": 360640,
"volumeDelta": -1.95
},
"lMusic": {
"name": null,
"id": 1233393309,
"size": 4328430,
"extension": "mp3",
"sr": 44100,
"dfsId": 3265549590228201,
"bitrate": 96000,
"playTime": 360640,
"volumeDelta": -2.02
},
"bMusic": {
"name": null,
"id": 1233393309,
"size": 4328430,
"extension": "mp3",
"sr": 44100,
"dfsId": 3265549590228201,
"bitrate": 96000,
"playTime": 360640,
"volumeDelta": -2.02
},
"rtype": 0,
"rurl": null,
"mp3Url": "http://m2.music.126.net/zVM6-lB27maJGaFaMslCNA==/3265549590228201.mp3",
"mvid": 482007
}
],
"trackCount": 18,
"createTime": 1472282018713,
"playCount": 28,
"userId": 76521820,
"newImported": false,
"privacy": 0,
"specialType": 0,
"updateTime": 1477208303380,
"commentThreadId": "A_PL_0_452433351",
"coverImgUrl": "http://p4.music.126.net/b-TEytnobA_J4Z7m05tfPw==/1401877340850803.jpg?imageView2/2/w/1280",
"coverImgId": 1401877340850803,
"totalDuration": 0,
"trackUpdateTime": 1477208303389,
"highQuality": false,
"status": 0,
"trackNumberUpdateTime": 1472282018996,
"adType": 0,
"description": null,
"tags": [],
"cloudTrackCount": 0,
"subscribedCount": 0,
"name": "冬 未了",
"id": 452433351,
"shareCount": 0,
"commentCount": 0
},
"code": 200
}
数据中能想到的需要的字段,基本都能满足,比如歌单信息,创建时间、更新时间、播放次数、分享次数、歌曲列表,歌曲专辑,歌曲发行公司、专辑封面等……
接下来json_decode()
解析数据成数组并展示。
<br />歌单名称:<?=$playlist['result']['name']?>
<br />更新时间:<?php echo date("Y-m-d H:i:s",$playlist['result']['updateTime']/1000);?>
<br />歌单封面:<img src="<?=$playlist['result']['coverImgUrl']?>" alt="" width="100" />
<br />歌曲数量:<?php echo count($playlist['result']['tracks'])?>
<br />歌曲列表:<br />
<?php foreach($playlist['result']['tracks'] as $row){
echo ' ';
echo $row['name'];
echo ' - ';
echo $row['artists'][0]['name'];
echo '<br />';
}
得到:
其中很多字段没有展示出来,比如专辑名,专辑封面等,可以从上面的json中自己获取修改一下代码得到。 最重要的播放地址字段,在json中是mp3Url,格式是mp3扩展名文件,可以下载或播放。
另外,获取歌词,根据获取歌单接口中,歌曲list中,有一个字段是id,根据id可以获取歌曲的歌词,api是:
public function getAlbum($id){
$return = file_get_contents('http://music.163.com/api/song/lyric?id='.$id.'&lv=-1&kv=-1');
return json_decode($return,true);
}
得到的json是:
{
"sgc": true,
"sfy": false,
"qfy": false,
"lrc": {
"version": 25,
"lyric": "[00:01.570]在这个世界上\n[00:03.610]没有不会消失的身影\n[00:06.530]但是某些忘不了的记忆\n[00:09.490]却永远在心中\n[00:12.690]我跟家凯最大的遗憾\n[00:15.770]都是爸爸因为生病\n[00:17.840]从来没有看过我们的演唱会\n[00:21.360]我们的生命中\n[00:23.500]一定都有很多舍不得\n[00:24.990]却得离开的人\n[00:28.630]当我们抬起头\n[00:30.160]看见星星\n[00:31.700]就会想起所有思念的人\n[00:34.700]\n[00:37.200] Look at the stars\n[00:43.540]look how they shine for you\n[00:51.860]And all the things you do\n[01:09.960]\n[01:28.120]\n[01:31.530]很想知道你近况\n[01:35.300]我听人说倒不如你对我讲\n[01:42.610]经过那段遗憾\n[01:47.510]请你放心 我变得更加坚强\n[01:54.950]世界不管怎样荒凉\n[02:01.380]爱过你就不怕孤单\n[02:05.280]\n[02:06.470]我最亲爱的 你过的怎么样\n[02:12.590]没我的日子 你别来无恙\n[02:18.800]依然亲爱的 我没让你失望\n[02:25.300]让我亲一亲 像过去一样\n[02:29.950]\n[02:34.190]我想你一定喜欢\n[02:37.900]现在的我学会了你最爱的开朗\n[02:46.500]想起你的模样\n[02:51.000]有什么错 还不能够被原谅\n[02:58.610]世界不管怎样荒凉\n[03:04.740]爱过你就不怕孤单\n[03:08.840]\n[03:10.180]我最亲爱的 你过的怎么样\n[03:16.270]没我的日子 你别来无恙\n[03:22.329]依然亲爱的 我没让你失望\n[03:28.940]让我亲一亲 像朋友一样\n[03:33.940]\n[03:49.980]虽然离开了你的时间\n[03:54.900]比一起还漫长\n[03:58.660]我们总能补偿\n[04:02.410]因为中间空白的时光\n[04:07.380]如果还能分享 也是一种浪漫\n[04:15.300]关系虽然不再一样\n[04:20.850]关心却怎么能说断就断\n[04:28.260]\n[04:30.100]我最亲爱的 你过的怎么样\n[04:36.460]没我的日子 你别来无恙\n[04:42.610]依然亲爱的 我没让你失望\n[04:48.960]让我亲一亲 像亲人一样\n[04:54.240]\n[04:55.510]我最亲爱的 你过的怎么样\n[05:01.900]没我的日子 你别来无恙\n[05:08.210]依然亲爱的 我没让你失望\n[05:14.580]让我亲一亲 像过去一样\n[05:19.820]\n[06:10.100]\n"
},
"klyric": {
"version": 6,
"lyric": "[ti:我最亲爱的]\n[ar:苏打绿]\n[by:123933@网易云音乐_2]\n[#:http://music.163.com/#/song?id=26075399]\n[90874,12751](0,203)很(0,254)想(0,204)知(0,254)道(0,255)你(0,355)近(0,1726)况(0,203)我(0,254)听(0,255)人(0,1268)说(0,1067)还(0,559)不(0,305)如(0,407)你(0,762)对(0,458)我(0,3962)讲\n[103625,11022](0,203)经(0,254)过(0,305)那(0,254)段(0,305)遗(0,2184)憾(0,405)请(0,357)你(0,1268)放(0,1626)心(0,203)我(0,356)变(0,355)得(0,308)更(0,406)加(0,406)坚(0,1827)强\n[114647,11737](0,509)世(0,407)界(0,357)不(0,508)管(0,354)怎(0,456)样(0,410)荒(0,2996)凉(0,761)爱(0,510)过(0,356)你(0,457)就(0,406)不(0,456)怕(0,356)孤(0,2438)单\n[126384,6250](0,253)我(0,305)最(0,204)亲(0,355)爱(0,969)的(0,202)你(0,254)过(0,407)的(0,356)怎(0,252)麼(0,2693)样\n[132634,6351](0,254)没(0,405)我(0,256)的(0,407)日(0,1117)子(0,306)你(0,306)别(0,304)来(0,406)无(0,2590)恙\n[138985,6450](0,356)依(0,355)然(0,205)亲(0,407)爱(0,861)的(0,307)我(0,254)没(0,406)让(0,355)你(0,354)失(0,2590)望\n[145435,9346](0,254)让(0,406)我(0,254)亲(0,202)一(0,305)亲(0,864)像(0,508)过(0,507)去(0,508)一(0,5538)样\n[154781,6501](0,253)我(0,204)想(0,303)你(0,304)一(0,355)定(0,610)喜(0,1117)欢(0,255)现(0,354)在(0,357)的(0,2389)我\n[161282,6095](0,203)学(0,305)会(0,306)了(0,559)你(0,358)最(0,354)爱(0,304)的(0,456)开(0,3250)朗\n[167377,7415](0,254)想(0,255)起(0,254)你(0,353)的(0,357)模(0,2235)样(0,203)要(0,354)什(0,1675)麼(0,1475)错\n[174792,3705](0,252)(0,356)还(0,305)不(0,203)能(0,202)够(0,306)被(0,457)原(0,1624)谅\n[178497,6805](0,457)世(0,407)界(0,355)不(0,407)管(0,406)怎(0,355)样(0,407)荒(0,4011)凉\n[185302,5083](0,407)爱(0,406)过(0,406)你(0,407)就(0,406)不(0,407)怕(0,408)孤(0,2236)单\n[190385,6246](0,253)我(0,305)最(0,204)亲(0,406)爱(0,1016)的(0,253)你(0,254)过(0,305)的(0,355)怎(0,309)麼(0,2586)样\n[196631,6344](0,356)没(0,406)我(0,254)的(0,355)日(0,1016)子(0,406)你(0,405)别(0,255)来(0,405)无(0,2486)恙\n[202975,5997](0,406)依(0,355)然(0,255)亲(0,508)爱(0,916)的(0,254)我(0,508)没(0,355)让(0,409)你(0,456)失(0,1575)望\n[208972,20812](0,609)让(0,457)我(0,305)亲(0,405)一(0,864)亲(0,253)像(0,660)朋(0,611)友(0,304)一(0,16344)样\n[229784,5182](0,202)虽(0,408)然(0,355)离(0,457)开(0,456)了(0,407)你(0,457)的(0,305)时(0,2135)间\n[234966,3858](0,354)一(0,408)起(0,1318)还(0,306)漫(0,1472)长\n[238824,3757](0,304)我(0,560)们(0,457)总(0,354)能(0,406)补(0,1676)偿\n[242581,4977](0,305)因(0,406)为(0,356)中(0,507)间(0,408)空(0,457)白(0,355)的(0,356)时(0,1827)光\n[247558,4062](0,356)如(0,304)果(0,306)还(0,1167)能(0,406)分(0,1523)享\n[251620,4164](0,508)也(0,458)是(0,355)一(0,304)种(0,559)浪(0,1980)漫\n[255784,5028](0,356)关(0,407)系(0,405)虽(0,457)然(0,407)不(0,407)再(0,304)一(0,2285)样\n[260812,9504](0,1425)关(0,407)心(0,355)却(0,508)怎(0,406)能(0,508)说(0,559)断(0,2946)就(0,2390)断\n[270316,6753](0,305)我(0,355)最(0,203)亲(0,407)爱(0,964)的(0,355)你(0,408)过(0,457)的(0,255)怎(0,1014)麼(0,2030)样\n[277069,5744](0,254)没(0,254)我(0,204)的(0,356)日(0,812)子(0,409)你(0,357)别(0,304)来(0,458)无(0,2336)恙\n[283524,5890](0,355)依(0,203)然(0,355)亲(0,356)爱(0,560)的(0,304)我(0,204)没(0,304)让(0,304)你(0,406)失(0,2539)望\n[289414,6453](0,358)让(0,405)我(0,203)亲(0,457)一(0,814)亲(0,204)像(0,609)亲(0,560)人(0,406)一(0,2437)样\n[295867,6351](0,356)我(0,355)最(0,254)亲(0,356)爱(0,914)的(0,305)你(0,254)过(0,254)的(0,456)怎(0,203)麼(0,2644)样\n[302218,6396](0,353)没(0,406)我(0,254)的(0,408)日(0,1014)子(0,356)你(0,405)别(0,203)来(0,510)无(0,2487)恙\n[308614,6400](0,355)依(0,407)然(0,203)亲(0,457)爱(0,863)的(0,355)我(0,204)没(0,406)让(0,255)你(0,456)失(0,2439)望\n[315014,6909](0,407)让(0,356)我(0,253)亲(0,408)一(0,761)亲(0,255)像(0,508)过(0,559)去(0,407)一(0,2995)样\n"
}
"code": 200
}
两种歌词形式,第一种Lrc单行歌词,第二种是逐字歌词。至于解析的话,网上应该有很多js库可以用。
暂时就这么多,下面是php的demo代码,有兴趣可以下载研究。 点击下载