Heim >Backend-Entwicklung >PHP-Tutorial >PHP获取QQ音乐的API类

PHP获取QQ音乐的API类

WBOY
WBOYOriginal
2016-07-25 08:49:501556Durchsuche
QQ soso音乐搜索api 演示地址:http://www.ip62.com/qqmusic/ 点击mp3地址后面的直接在线播放
  1. class music {
  2. private $musicname;
  3. public function __construct($musicname){
  4. $this->musicname = $musicname;
  5. }
  6. private function map_url(){
  7. $url = "http://shopcgi.qqmusic.qq.com/fcgi-bin/shopsearch.fcg?value=".urlencode(iconv("utf-8","gb2312",$this->musicname));
  8. if(!function_exists("file_get_contents"))
  9. {
  10. $ch = curl_init();
  11. $timeout = 5;
  12. curl_setopt ($ch, CURLOPT_URL, $url);
  13. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  14. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  15. $file_contents = curl_exec($ch);
  16. curl_close($ch);
  17. }else{
  18. $file_contents = file_get_contents($url);
  19. }
  20. return $file_contents;
  21. }
  22. public function getmusic(){
  23. $data = $this-> map_url();
  24. $data = substr($data,15);
  25. $data = substr($data,0,-2);
  26. preg_match("/songlist\:\[(?P.*)\]\}/i", $data,$musicdata);
  27. $musicdata = explode(",",$musicdata['music']);
  28. $music = array();
  29. foreach($musicdata as $v){
  30. if(preg_match("/\{idx\:(?P.*)/i",$v,$a)){
  31. $id = trim($a[id],"\"");
  32. }
  33. if(preg_match("/song_id\:(?P.*)/i", $v,$c)){
  34. $music[$id]['song_id'].=trim($c['song_id'],"\"");
  35. }
  36. if(preg_match("/song_name\:(?P.*)/i",$v,$s)){
  37. $music[$id]['song_name'].=trim($s['song_name'],"\"");
  38. }
  39. if(preg_match("/album_name\:(?P.*)/i",$v,$n)){
  40. $music[$id]['album_name'].=trim($n['album_name'],"\"");
  41. }
  42. if(preg_match("/singer_name\:(?P.*)/i",$v,$name)){
  43. $music[$id]['singer_name'].=trim($name['singer_name'],"\"");
  44. }
  45. if(preg_match("/location\:(?P.*)/i",$v,$l)){
  46. $music[$id]['location'].=trim($l['location'],"\"");
  47. }
  48. }
  49. return $music;
  50. }
  51. public function getmusicurl(){
  52. $muiscurl = "";
  53. $result = $this->getmusic();
  54. foreach ($result as $id =>$v){
  55. $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
    ";
  56. }
  57. return $muiscurl;
  58. }
  59. }
  60. $music = new music("新年快乐");
  61. $data = $music->getmusic();
  62. //var_dump($data);
  63. //echo "http://stream1{$data[1]['localtion']}.qqmusic.qq.com/3{$data[1]['song_id']}.mp3";
  64. echo $music->getmusicurl();
  65. //演示地址:http://www.ip62.com/qqmusic/
  66. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn