PHP获取QQ音乐的API类,可以集成到各种音乐网站上使用

widuu 2013-6-18 PHP技术 11,953 11 喜欢 (6)

这个我在开发一个微信公众平台的时候,想做个音乐的系统,由于本身没有资源所以自己就去简单的去做了一下,然后简单的封装了一下,供以后的人参考,这个类只是作为参考用,然后大家需要什么功能然后自己扩展就可以了,其实没有什么太难的,这个是做的QQ音乐的API,其实没什么难的,大家简单应用下还是不错的,可以关注我的微度生活微信公众平台,搜索“歌曲+歌名”体验一下!废话不多说了上代码!
[php]
<?php
/**
* music.class.php 音乐API类
*
* @copyright widuu
* @license http://www.widuu.com
* @lastmodify 2013-6-18
*/
class music {
private $musicname;

public function __construct($musicname){
$this->musicname = $musicname;
}
private function map_url(){

$url = "http://shopcgi.qqmusic.qq.com/fcgi-bin/shopsearch.fcg?value=".urlencode(iconv("utf-8","gb2312",$this->musicname));
if(!function_exists("file_get_contents"))
{
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}else{
$file_contents = file_get_contents($url);
}
return $file_contents;
}

public function getmusic(){
$data = $this-> map_url();
$data = substr($data,15);
$data = substr($data,0,-2);
preg_match("/songlist\:\[(?P<music>.*)\]\}/i", $data,$musicdata);
$musicdata = explode(",",$musicdata['music']);
$music = array();
foreach($musicdata as $v){
if(preg_match("/\{idx\:(?P<id>.*)/i",$v,$a)){
$id = trim($a[id],"\"");
}
if(preg_match("/song_id\:(?P<song_id>.*)/i", $v,$c)){
$music[$id]['song_id'].=trim($c['song_id'],"\"");
}
if(preg_match("/song_name\:(?P<song_name>.*)/i",$v,$s)){
$music[$id]['song_name'].=trim($s['song_name'],"\"");
}
if(preg_match("/album_name\:(?P<album_name>.*)/i",$v,$n)){
$music[$id]['album_name'].=trim($n['album_name'],"\"");
}
if(preg_match("/singer_name\:(?P<singer_name>.*)/i",$v,$name)){
$music[$id]['singer_name'].=trim($name['singer_name'],"\"");
}
if(preg_match("/location\:(?P<location>.*)/i",$v,$l)){
$music[$id]['location'].=trim($l['location'],"\"");
}
}
return $music;
}
public function getmusicurl(){
$muiscurl = "";
$result = $this->getmusic();
foreach ($result as $id =>$v){

$muiscurl.="歌曲{$id},歌曲名称:".iconv("gb2312","utf-8",$v['song_name']).",歌手:".iconv('gb2312','utf-8',$v['singer_name']).",专辑:".iconv('gb2312','utf-8',$v['album_name']).",歌曲地址:http://stream1{$v['location']}.qqmusic.qq.com/3{$v['song_id']}.mp3<br>";
}
return $muiscurl;
}
}
$music = new music("新年快乐");
$data = $music->getmusic();
//var_dump($data);
//echo "http://stream1{$data[1]['localtion']}.qqmusic.qq.com/3{$data[1]['song_id']}.mp3";
echo $music->getmusicurl();
[/php]

大家希望开发成什么功能留言即可,有什么不足可以留言,希望转载的时候留下版权,谢谢大家!
微信公众平台

转载请注明来自微度网络,本文标题:《PHP获取QQ音乐的API类,可以集成到各种音乐网站上使用》

喜欢 6 发布评论
Top