查詢QQ音樂是很早前就出來的一個介面。
這裡使用jQuery和jPlayer來實現QQ空間音樂的查詢。
我們要使用的介面位於bejson介面頁面中的音樂介面欄位。
QQ音樂介面位址:
http://qzone-music.qq.com/fcg-bin/fcg_music_fav_getinfo.fcg?dirinfo=0&dirid=1&uin=QQ號&p=0.51963827254726262472638245196382725472626261263823852638238263826238526382623826380 :
1.gtk參數的取得方式
f
unction getGTK() { var str = "@HR3etVm80"; var hash = 5381; for (var i = 0, len = str.length; i < len; ++i) { hash += (hash << 5) + str.charAt(i).charCodeAt(); } var gtk = hash & 0x7fffffff; //document.getElementById("gtk").value = gtk; return gtk; }
2.請QQ空間介面
function getMusicId() { var qqNo = document.getElementById("qqNo").value; var url = 'http://qzone-music.qq.com/fcg-bin/cgi_playlist_xml.fcg?uin=' + qqNo + '&json=1&g_tk=' + getGTK(); $.getScript(url); }
3.回呼拼裝JSON
根據回傳的J
function jsonCallback(data) { if(data.code==1){ alert(data.msg); return; } var songs = data.qqmusic.playlist.song; var dataStr = "["; for (var i = 0; i < songs.length; i++) { dataStr += "{"; dataStr += "title:'" + songs[i].xsong_name + "',"; dataStr += "mp3:'" + songs[i].xsong_url + "'"; dataStr += "}"; if (i < songs.length) { dataStr += ','; } } dataStr += ']'; eval("ds=" + dataStr); newPlayer(ds); }
var playList; function newPlayer(data) { playList = new jPlayerPlaylist({ jPlayer: "#jquery_jplayer_1", cssSelectorAncestor: "#jp_container_1" }, data, { swfPath: "js", supplied: "mp3", wmode: "window" }); }