2017-07-20 80 views
3

我想從使用PHP和JQuery的Shoutcast服務器獲取歌曲歷史記錄。我已經進行到PHP文件的Ajax調用包含從流server.The的Jquery取得最近播放的歌曲代碼是我的應用程序文件夾如何從Shoutcast服務器使用PHP返回最近播放的歌曲

jQuery的內寫上:

$.ajax({ 
     type: "GET", 
     url: "http://example.com/recent_songs.php", 
     headers: {'Access-Control-Allow-Origin': '*'}, 
     success: function(response){ 
      console.log(response); 
      return false; 
     }, 
     error: function(error){ 
      console.log(error); 
      console.log("Network Error!"); 
     } 
    }); 

的PHP文件駐留在與Jquery文件不同的服務器。

PHP(recent_songs.php):

$handle = ''; 
$handle = get_data(); 

function get_data() { 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0'); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 8); 

     $data_string = ""; 
     function write_function($handle, $data) { 
      global $data_string; 
      $data_string .= $data; 
      if (strlen($data_string) > 200) { 
       return 0; 
      } 
      else { 
       return strlen($data); 
      } 
     } 
     curl_setopt($ch, CURLOPT_URL, 'ip:port/played.html'); 
     $data = curl_exec($ch);   
     $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   
     if ($code!='200'){ 
      return 'error'; 
     }else{ 
      return 'success';   
} 

它總是返回HTTP代碼0和從SHOUTcast服務器空響應。然而,當我在瀏覽器中運行這個URL:ip:port/played.html它顯示了完整的歌曲歷史。有什麼想法從shoutcast服務器獲得響應。

任何形式的幫助將不勝感激。謝謝!

+0

想必'IP:port'只是佔位符和你實際使用的IP地址和端口號? – RamRaider

+0

是的我實際上使用shoutcast的IP地址和端口號。 –

回答

1
/* Frisky Radio */ 
$ip='50.7.64.226'; 
$port=8000; 

function get_data($ip=false, $port=false) { 
    try{ 
     if($ip && $port){ 
      $ch = curl_init(); 

      curl_setopt($ch, CURLOPT_URL, "{$ip}:{$port}/played.html"); 
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0'); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4); 
      curl_setopt($ch, CURLOPT_TIMEOUT, 8); 

      $data=(object)array(
       'response' => curl_exec($ch), 
       'info'  => (object)curl_getinfo($ch) 
      ); 

      return $data; 
     } 

     throw new Exception('Ip address and port number are required'); 
    }catch(Exception $e){ 
     echo $e->getMessage(); 
    } 
} 


$data=get_data($ip, $port); 
if($data->info->http_code==200){ 
    echo '<pre>',print_r($data->response,true),'</pre>'; 
} 

上述(略編輯)的輸出是:

02:09:28 FRISKY | RPO Records Session - March 2017 - Oscar Vasquez Current Song 
01:09:29 FRISKY | Rogue - July 2017 - Amber Long 
00:09:32 FRISKY | Feelin FRISKY - June 2017 - Strachan 
22:06:31 FRISKY | Voyager - July 2017 - Deepsense 
20:02:07 FRISKY | Fatalist - July 2017 - Digital Department 
18:00:47 FRISKY | 6th Auditorium - July 2017 - Andrea Cassino 
17:02:07 FRISKY | Perspectives - July 2017 - Paul Kardos 
16:02:11 FRISKY | Perspectives - July 2017 - Darin Epsilon 
15:06:33 FRISKY | Hazendonk FM - July 2017 - Paul Hazendonk 
14:03:26 FRISKY | Southside - July 2017 - Graziano Raffa 

可替代地,與響應數據可以使用DOMDocumentDOMXPath抓住表顯示數據和某處頁面中使用。 因此,與迴應:

/* Not all shoutcast servers have a list of played songs */ 
$table='No results'; 

$dom=new DOMDocument(); 
$dom->loadHTML($data->response); 

$xp=new DOMXPath($dom); 
$col=$xp->query('//table[2]'); 


if(!empty($col)){ 
    $doc=new DOMDocument(); 
    $doc->appendChild($doc->importNode($col->item(0),true)); 
    $table=$doc->saveHTML(); 
} 

$dom = $doc = $xp = $col = null; 

echo $table; 
+0

我試過你提供的代碼。但在這兩種情況下(我的和你的),curl_exec()在我的服務器上返回false,它返回http_code 0,即使所有選項都是相同的。你有什麼想法嗎? –

+0

你試圖達到什麼IP /端口?它是在互聯網上還是本地服務器上? – RamRaider

+0

我正在使用實時IP和端口連接shoutcast。是的,它是在interweb –

相關問題