使用 PHP 显示 Icecast2 统计数据

发布于 2024-09-14 22:42:40 字数 163 浏览 5 评论 0原文

我在使用 PHP 查看统计数据(观众、当前播放的歌曲等)时遇到一些问题,并且找不到任何如何执行此操作的信息。

Icecast2 中包含几个 XLS 文件,我可以使用 PHP 将此文件包含到我的网站中,但我不想更新包含每 5 秒一次的 DIV,并且不适用于 XLS 文件。

谢谢!

I have some trouble viewing statistics (viewers, current song playing etc) with PHP and I can't find any information how to do this.

There is a couple of XLS files included with Icecast2 and I could include this files with PHP to my site but I wan't to update the DIV that the include is in every 5 seconds and that won't work with XLS files.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彩虹直至黑白 2024-09-21 22:42:40

您好,感谢您的代码。
我用它创建了一个类并添加了一些检查,这样当服务器离线时它就不会抱怨。
因为我从这里拿走它,所以我将分享该课程:

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

}
?>

Hi there and thanks for the code.
I made a class from it and added some checks so it doesn't complain when the server is offline.
Since I took it from here I will share the class back:

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

}
?>
老子叫无熙 2024-09-21 22:42:40

通过使用这段代码,我已经让它工作了:

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

?> 

By using this code I've got it working:

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

?> 
黯然 2024-09-21 22:42:40

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

Please take a look at my answer over here why it is a very bad idea to use this script.
tl;dr: You should not parse the Icecast HTML status page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文