2010-08-15 56 views
7

我在使用PHP查看统计信息(观看者,当前歌曲播放等)时遇到了一些麻烦,我无法找到任何信息如何执行此操作。使用PHP来显示Icecast2的统计信息

Icecast2中包含了几个XLS文件,我可以用PHP将这些文件包含到我的网站中,但是我不想更新包含在每5秒钟内的DIV,并且这不适用于XLS文件。

谢谢!

回答

14

您好,感谢您的代码。 我从它做了一个类,并添加了一些检查,所以它不会抱怨当服务器脱机。 因为我把它从这里我将分享类回:

<?php 

class IceCast { 
    var $server = "http://localhost:8000"; 
    var $stats_file = "/status.xsl"; 
    var $radio_info=array(); 

    function __construct() { 
     //build array to store our radio stats for later use   
     $this->radio_info['server'] = $this->server; 
     $this->radio_info['title'] = 'Offline'; 
     $this->radio_info['description'] = 'Radio offline'; 
     $this->radio_info['content_type'] = ''; 
     $this->radio_info['mount_start'] = ''; 
     $this->radio_info['bit_rate'] = ''; 
     $this->radio_info['listeners'] = ''; 
     $this->radio_info['most_listeners'] = ''; 
     $this->radio_info['genre'] = ''; 
     $this->radio_info['url'] = ''; 
     $this->radio_info['now_playing'] = array(); 
     $this->radio_info['now_playing']['artist'] = 'Unknown'; 
     $this->radio_info['now_playing']['track'] = 'Unknown'; 
    } 

    function setUrl($url) { 
     $this->server=$url; 
     $this->radio_info['server'] = $this->server; 
    } 

    private function fetch() { 
     //create a new curl resource 
     $ch = curl_init(); 

     //set url 
     curl_setopt($ch,CURLOPT_URL,$this->server.$this->stats_file); 

     //return as a string 
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 

     //$output = our stauts.xsl file 
     $output = curl_exec($ch); 

     //close curl resource to free up system resources 
     curl_close($ch); 

     return $output; 
    } 

    function getStatus() { 
     $output=$this->fetch(); 

     //loop through $ouput and sort into our different arrays 
     $temp_array = array(); 

     $search_for = "<td\s[^>]*class=\"streamdata\">(.*)<\/td>"; 
     $search_td = array('<td class="streamdata">','</td>'); 


     if(preg_match_all("/$search_for/siU",$output,$matches)) { 
      foreach($matches[0] as $match) { 
       $to_push = str_replace($search_td,'',$match); 
       $to_push = trim($to_push); 
       array_push($temp_array,$to_push); 
      } 
     } 

     if(count($temp_array)) { 
      //sort our temp array into our ral array 
      $this->radio_info['title'] = $temp_array[0]; 
      $this->radio_info['description'] = $temp_array[1]; 
      $this->radio_info['content_type'] = $temp_array[2]; 
      $this->radio_info['mount_start'] = $temp_array[3]; 
      $this->radio_info['bit_rate'] = $temp_array[4]; 
      $this->radio_info['listeners'] = $temp_array[5]; 
      $this->radio_info['most_listeners'] = $temp_array[6]; 
      $this->radio_info['genre'] = $temp_array[7]; 
      $this->radio_info['url'] = $temp_array[8]; 

      if(isset($temp_array[9])) { 
       $x = explode(" - ",$temp_array[9]); 
       $this->radio_info['now_playing']['artist'] = $x[0]; 
       $this->radio_info['now_playing']['track'] = $x[1]; 
      } 
     } 
     return $this->radio_info; 
     } 

} 
?> 
+0

非常有用!谢谢 – 2013-02-19 20:35:44

+1

这是一个非常糟糕的主意,因为它有一个重新制作的网络界面,所以这将与Icecast版本1.4打破。 – ePirat 2014-05-04 20:19:21

6

通过使用此代码中,我得到它的工作:

<?php 

/* 
* SCRIPT CONFIGURATIONS 
*/ 
$SERVER = 'http://myserver.com:8000'; //URL TO YOUR ICECAST SERVER 
$STATS_FILE = '/status.xsl'; //PATH TO STATUS.XSL PAGE YOU CAN SEE IN YOUR BROWSER (LEAVE BLANK UNLESS DIFFERENT) 

///////////////////// END OF CONFIGURATION --- DO NOT EDIT BELOW THIS LINE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 

//create a new curl resource 
$ch = curl_init(); 

//set url 
curl_setopt($ch,CURLOPT_URL,$SERVER.$STATS_FILE); 

//return as a string 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 

//$output = our stauts.xsl file 
$output = curl_exec($ch); 

//close curl resource to free up system resources 
curl_close($ch); 

//build array to store our radio stats for later use 
$radio_info = array(); 
$radio_info['server'] = $SERVER; 
$radio_info['title'] = ''; 
$radio_info['description'] = ''; 
$radio_info['content_type'] = ''; 
$radio_info['mount_start'] = ''; 
$radio_info['bit_rate'] = ''; 
$radio_info['listeners'] = ''; 
$radio_info['most_listeners'] = ''; 
$radio_info['genre'] = ''; 
$radio_info['url'] = ''; 
$radio_info['now_playing'] = array(); 
    $radio_info['now_playing']['artist'] = ''; 
    $radio_info['now_playing']['track'] = ''; 

//loop through $ouput and sort into our different arrays 
$temp_array = array(); 

$search_for = "<td\s[^>]*class=\"streamdata\">(.*)<\/td>"; 
$search_td = array('<td class="streamdata">','</td>'); 

if(preg_match_all("/$search_for/siU",$output,$matches)) { 
    foreach($matches[0] as $match) { 
     $to_push = str_replace($search_td,'',$match); 
     $to_push = trim($to_push); 
     array_push($temp_array,$to_push); 
    } 
} 

//sort our temp array into our ral array 
$radio_info['title'] = $temp_array[0]; 
$radio_info['description'] = $temp_array[1]; 
$radio_info['content_type'] = $temp_array[2]; 
$radio_info['mount_start'] = $temp_array[3]; 
$radio_info['bit_rate'] = $temp_array[4]; 
$radio_info['listeners'] = $temp_array[5]; 
$radio_info['most_listeners'] = $temp_array[6]; 
$radio_info['genre'] = $temp_array[7]; 
$radio_info['url'] = $temp_array[8]; 

$x = explode(" - ",$temp_array[9]); 
$radio_info['now_playing']['artist'] = $x[0]; 
$radio_info['now_playing']['track'] = $x[1]; 

?> 
2

为什么它是一个非常糟糕的主意使用这个脚本请看看我的答案over here
tl; dr:你不应该解析Icecast HTML状态页面。