PHP - 正在更新页面上的 Icecast 信息

PHP - Icecast info being updated on page

我正在使用来自 this 站点的脚本。这个脚本对我来说工作正常,它完成了它需要做的事情,但我有一个问题。当曲目在我的 Icecast 服务器上完成时,它不会在站点上获得更新。因此,如果我的歌曲是 'Stole the show' 而不是它说 'Stole the show' 页面但是当歌曲结束时,例如'Thinking out loud' 启动页面时仍然显示 'Stole the show' 刷新它会更新。但是如何让页面自动更新,这样用户就不必手动刷新了?

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;
    }

}
?>

首先,我必须指出你不应该使用这个脚本。它通过解析 Icecast 状态页面来工作,我们非常不鼓励这样做,因为它可能会改变。例如,在 Icecast 2.4 中,我们重新制作了完整的 Web 界面,所以这个脚本很可能会中断。

您实际上应该解析 Icecast 在 http://icecast.tld:8000/admin/stats 提供的 XML。它包含您需要的一切。如果您由于某种原因无法访问 Icecast 的管理页面,您可以使用 http://icecast.tld:8000/status-json.xsl 上的 JSON,它自 Icecast 2.4 以来就完全用于您描述的目的。

要让站点显示新的元数据信息而不刷新,您需要使用 AJAX 调用,它可以直接加载 status-json.xsl 并提取元数据并在页面上更新它,或者如果您使用管理员 XML 您需要编写一个 PHP 脚本,其中 returns json,您可以通过 AJAX 获取并相应地更新。

过去很多人都谈到了设置 node.js(如果您有服务器进行流式传输)。

就我个人而言,我采用了 jquery 解决方案;它只是每 10 秒将最后获取的数据与实时数据进行比较。这样它几乎可以加载 'real time'.

你可以在这里找到我的解决方案here http://www.radiodj.ro/community/index.php?topic=7471.0