2015-07-01 21 views
0

我正在使用來自this網站的腳本。這個腳本對我來說工作得很好,它完成了它需要做的事情,但我遇到了一個問題。當我的Icecast服務器上的曲目完成時,它不會在網站上獲得更新。因此,如果我的歌是'偷走了節目',而不是'竊取節目'這個頁面,但是當歌曲完成並且例如'大聲思考'開始的頁面仍然說'偷走秀'刷新後會更新。但是,如何使頁面自動更新以便用戶不必手動刷新?PHP - 正在更新的Icecast信息頁

PHP

<?php 
    // include the class file 
    include('icecast.php'); 

    // instantiate class 
    $stream = new IceCast(); 

    // set server and mount 
    $server = 'http://radio.finioxfm.com:8000'; 
    $file = '/status.xsl'; 

    // set the url 
    $stream->setUrl($server,$file); 

    // get status info 
    $radio = $stream->getStatus(); 

    // assign array to variables 
    extract($radio); 

    // echo the status 
    echo $status.'<br/>'; 

    // display more stats if ON AIR 
    if ($status=='ON AIR') : 
    echo $listeners.' listeners<br/>'; 
    echo $title.'<br/>'; 
    echo $genre.'<br/>'; 
    for ($i=0; $i < 1; $i++) { 
     echo $now_playing['artist'].'<br/>'; 
     echo $now_playing['track'].'<br/>'; 
    } 
    endif; 
?> 

icecast.php腳本

<?php 
class IceCast { 
var $server = "http://radio.finioxfm.com:8000"; 
var $stats_file = "/status.xsl"; 
var $radio_info=array(); 

function __construct() { 
    // build array to store our Icecast stats 
    $this->radio_info['server'] = $this->server; 
    $this->radio_info['title'] = ''; 
    $this->radio_info['description'] = ''; 
    $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'; 
    $this->radio_info['status'] = 'OFF AIR'; 
} 

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

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

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

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

    // $output = the status.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 $output and sort 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]; 
     } 
     $this->radio_info['status'] = 'ON AIR'; 

    } 
    return $this->radio_info; 
    } 

} 
?> 

回答

3

首先,我要指出的是,你不應該使用這個腳本。它通過解析Icecast Status頁面來工作,我們非常反對,因爲它可能會改變。例如在Icecast 2.4中,我們重新制作了完整的Web界面,所以很有可能這個腳本中斷了。

您應該實際解析XML Icecast提供的http://icecast.tld:8000/admin/stats。它包含你需要的一切。如果您出於某種原因無法訪問Icecast的管理頁面,則可以使用http://icecast.tld:8000/status-json.xsl的JSON,這是自Icecast 2.4以來出現的目的。

要讓站點顯示新的元數據信息而不刷新,您需要使用AJAX調用,它可以直接加載status-json.xsl並提取元數據並在頁面上更新它,或者如果您使用需要編寫的管理XML返回json的PHP腳本,您可以通過AJAX獲取並相應更新。

+0

謝謝您的回答。我可以進入管理員/統計信息文件,但是如何從該頁面將完整信息存入我的網站? –

1

過去很多人都談過如何設置node.js(如果你有一臺服務器在做流媒體)。

我個人用jQuery解決方案去了;它只是每隔10秒比較最近讀取的數據和實時數據。這種方式幾乎「實時」加載。

您可以在這裏找到我的解決方案細分herehttp://www.radiodj.ro/community/index.php?topic=7471.0