使用PHP显示Icecast2统计信息 [英] Use PHP to show Icecast2 statistics

查看:94
本文介绍了使用PHP显示Icecast2统计信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用PHP查看统计信息(查看器,当前播放的歌曲等)时遇到了一些麻烦,而我找不到任何有关如何执行此操作的信息.

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

Icecast2包含几个XLS文件,我可以将该文件与PHP一起包含到我的站点中,但我不想每5秒更新一次包含该DIV的DIV,而该DIV不适用于XLS文件

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.

谢谢!

推荐答案

您好,感谢您提供的代码. 我从中创建了一个类,并添加了一些检查,因此当服务器处于脱机状态时它不会抱怨. 自从我从这里拿走它之后,我将与大家分享课程:

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

}
?>

这篇关于使用PHP显示Icecast2统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆