PHP - Icecast info being updated on page

2019-08-29 03:11发布

问题:

I'm using a script from this site. This script works fine for me and it does what its need to do but I have one problem. When a track finishes on my Icecast server it doesn't get updates on the site. So if my song is 'Stole the show' than it says 'Stole the show' the page but when the song finished and e.g. 'Thinking out loud' starts the page still says 'Stole the show' on a refresh it will update. But how to make it so the page auto updates itself so the users doesn't have to refresh manually?

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 script

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

}
?>

回答1:

First of all, I have to point out that you shouldn't use this script. It works by parsing the Icecast Status page, which we highly discourage, as it may change. For example in Icecast 2.4 we re-made the complete web interface, so chances are that this script breaks.

You should actually parse the XML Icecast provides at http://icecast.tld:8000/admin/stats. It contains everything you need. If you can't access Icecast's Admin page for some reason, you can use the JSON at http://icecast.tld:8000/status-json.xsl, which is there since Icecast 2.4 exactly for the purpose you describe.

To get the site display new metadata information without refreshing, you need to use an AJAX call which either loads directly the status-json.xsl and extracts the metadata and updates it on the page, or if you use the admin XML you need to write a PHP script which returns json, that you can fetch via AJAX and update accordingly.



回答2:

A lot of people in the past have spoken about setting up node.js (if you have a server doing your streaming).

Personally I have gone with a jquery solution; which just compares the last fetched data with the live data every 10 seconds. That way it loads in almost 'real time'.

You can find my solution here broken down here http://www.radiodj.ro/community/index.php?topic=7471.0



标签: php icecast