php.de

Zurück   php.de > Webentwicklung > PHP Einsteiger > PHP Tipps 2010

 
 
LinkBack Themen-Optionen Thema bewerten
Alt 22.03.2010, 12:21  
Neuer Benutzer
 
Benutzerbild von Netcrash
 
Registriert seit: 23.07.2009
Beiträge: 18
PHP-Kenntnisse:
Anfänger
Netcrash befindet sich auf einem aufstrebenden Ast
Standard [Erledigt] Icecast Statusseite auslesen und auf eigenem Webserver zwischenspeichern

Hallo,

ich habe folgendes Problem, ich habe meinen Webserver und meinen Streamserver (Internetradio) Icecast2 nun möchte ich dort die XML auslesen wo der Aktuelle Song e.t.c. abgerufen werden können.

Ein eigendliches Script hierfür habe ich bereits, allerdings ruft hier jeder User alle 30 Sekunden die Daten vom Streamserver selber ab und dies möchte ich verhindern in dem ich eine Datei kann auch xml sein auf meinem Webspace dafür ablege alle 30 Sekunden.

Hier ein paar Code-Schnipsel dazu wie es derzeit läuft:

radio.class.php
PHP-Code:
class radio{
    function 
init(){ // Initialisierung und erzeugung des Arrays mit den Werten
        
$x_xml $this->get_xml();
        
$this->parse_xml($x_xml);
    }
    function 
get_xml(){ // XML Status auslesen und alles unn�tige entfernen
//         return implode(file("icecast2.xml")); // Auskommentieren beim Einsatz, nur f�r Testzwecke

        
$sp = @fsockopen($this->host$this->port, &$errno, &$errstr10);

        if (!
$sp)
            die(
'Error while connecting to Server.');
        else{
            
set_socket_blocking($spfalse);

            if(
$this->serv == "shoutcast"){
                
fputs($sp"GET /admin.cgi?pass=" $this->pass "&mode=viewxml HTTP/1.1\nUser-Agent: Mozilla\n\n");
            }
            else if(
$this->serv == "icecast"){
                
fputs($sp"GET /admin/stats.xml HTTP/1.1\nUser-Agent: Mozilla\n");
                
fputs($sp"Host: " $this->host "\n");
                
fputs($sp"Authorization: Basic " base64_encode("admin:" $this->pass) . "\n\n");
            }
            else
                die(
'There is no Server selected. Please select \"shoutcast\" or \"icecast\".');

            for(
$i 0$i 30$i++) {
                if(
feof($sp))
                    break;
                
$sp_data .= fread($sp31337);
                
usleep(500000);
            }
        }
        if(
$this->serv == "shoutcast")
            
$sp_data ereg_replace("^.*<SHOUTCASTSERVER""<SHOUTCASTSERVER"$sp_data);
        if(
$this->serv == "icecast")
            
$sp_data ereg_replace("^.*<icestats""<icestats"$sp_data);

        return 
$sp_data;
    }
    function 
set_host($host){ // Host schreiben
        
$this->host $host;
    }
    function 
set_port($port){ // Port schreiben
        
$this->port $port;
    }
    function 
set_pass($pass){ // Passwort schreiben
        
$this->pass $pass;
    }
    function 
set_serv($serv){ // Serverart schreiben
        
$this->serv $serv;
    }
    function 
parse_xml($x_xml){ // XML Baum verarbeiten, Array mit Tags und Werten, Array mit Songhistory (Shoutcast) und Array mit H�rern (shoutcast) erzeugen
        
$x_data_flag FALSE;
        
$x_parser xml_parser_create();
        
xml_parse_into_struct($x_parser$x_xml$x_vals$x_index);
        
xml_parser_free($x_parser);

        
$this->x_vals $x_vals;
        
$this->x_index $x_index;

        if(
$this->serv == "shoutcast"){
            
$this->status $x_vals[$x_index['STREAMSTATUS'][0]]['value'];

            
$this->current_listeners $x_vals[$x_index['CURRENTLISTENERS'][0]]['value'];
            
$this->peak_listeners $x_vals[$x_index['PEAKLISTENERS'][0]]['value'];
            
$this->max_listeners $x_vals[$x_index['MAXLISTENERS'][0]]['value'];

            
$this->average_time $x_vals[$x_index['AVERAGETIME'][0]]['value'];

            
$this->server_genre $x_vals[$x_index['SERVERGENRE'][0]]['value'];
            
$this->server_url $x_vals[$x_index['SERVERURL'][0]]['value'];
            
$this->server_title $x_vals[$x_index['SERVERTITLE'][0]]['value'];

            
$x_title explode(" - "$x_vals[$x_index['SONGTITLE'][0]]['value']);

            
$this->artist $x_title[0];
            
$this->title $x_title[1];
            
$this->song_url $x_vals[$x_index['SONGURL'][0]]['value'];

            
$this->irc $x_vals[$x_index['IRC'][0]]['value'];
            
$this->aim $x_vals[$x_index['AIM'][0]]['value'];
            
$this->icq $x_vals[$x_index['ICQ'][0]]['value'];

            
$this->version $x_vals[$x_index['VERSION'][0]]['value'];

            
$this->listeners();
            
$this->song_history();
        }
        else if(
$this->serv == "icecast"){
            
$x_count 0;
            
$xa_count 0;

            while(
$x_index['SOURCE'][$x_count]){
                if(
$x_vals[$x_index['SOURCE'][$x_count]]['type'] == "open"){
                    
$x_array[] = $x_index['SOURCE'][$x_count];

                    
$this->mounts[$xa_count]['mount'] = $x_vals[$x_index['SOURCE'][$x_count]]['attributes']['MOUNT'];
                    
$this->mounts[$xa_count]['artist'] = $x_vals[$x_index['ARTIST'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['audio_bitrate'] = $x_vals[$x_index['AUDIO_BITRATE'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['audio_channels'] = $x_vals[$x_index['AUDIO_CHANNELS'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['audio_info'] = $x_vals[$x_index['AUDIO_INFO'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['audio_samplerate'] = $x_vals[$x_index['AUDIO_SAMPLERATE'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['bitrate'] = $x_vals[$x_index['BITRATE'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['genre'] = $x_vals[$x_index['GENRE'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['ice-bitrate'] = $x_vals[$x_index['ICE-BITRATE'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['ice-channels'] = $x_vals[$x_index['ICE-CHANNELS'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['ice-quality'] = $x_vals[$x_index['ICE-QUALITY'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['ice-samplerate'] = $x_vals[$x_index['ICE-SAMPLERATE'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['listeners'] = $x_vals[$x_index['LISTENERS'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['listenurl'] = $x_vals[$x_index['LISTENURL'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['public'] = $x_vals[$x_index['PUBLIC'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['server_description'] = $x_vals[$x_index['SERVER_DESCRIPTION'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['server_name'] = $x_vals[$x_index['SERVER_NAME'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['server_url'] = $x_vals[$x_index['SERVER_URL'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['subtype'] = $x_vals[$x_index['SUBTYPE'][$xa_count]]['value'];
                    
$this->mounts[$xa_count]['title'] = $x_vals[$x_index['TITLE'][$xa_count]]['value'];

                    if(
$this->mounts[$xa_count]['artist'] == ""){
                        
$artist explode(" - "$this->mounts[$xa_count]['title']);

                        
$this->mounts[$xa_count]['artist'] = $artist[0];
                        
$this->mounts[$xa_count]['title'] = $artist[1];
                    }
                    
$xa_count++;
                }
                
$x_count++;
            }
            if(
$this->mounts[0]['mount'] != ""){
                
$this->mount['mount'] = $x_vals[$x_index['SOURCE'][0]]['attributes']['MOUNT'];
                
$this->mount['artist'] = $x_vals[$x_index['ARTIST'][0]]['value'];
                
$this->mount['audio_bitrate'] = $x_vals[$x_index['AUDIO_BITRATE'][0]]['value'];
                
$this->mount['audio_channels'] = $x_vals[$x_index['AUDIO_CHANNELS'][0]]['value'];
                
$this->mount['audio_info'] = $x_vals[$x_index['AUDIO_INFO'][0]]['value'];
                
$this->mount['audio_samplerate'] = $x_vals[$x_index['AUDIO_SAMPLERATE'][0]]['value'];
                
$this->mount['bitrate'] = $x_vals[$x_index['BITRATE'][0]]['value'];
                
$this->mount['genre'] = $x_vals[$x_index['GENRE'][0]]['value'];
                
$this->mount['ice-bitrate'] = $x_vals[$x_index['ICE-BITRATE'][0]]['value'];
                
$this->mount['ice-channels'] = $x_vals[$x_index['ICE-CHANNELS'][0]]['value'];
                
$this->mount['ice-quality'] = $x_vals[$x_index['ICE-QUALITY'][0]]['value'];
                
$this->mount['ice-samplerate'] = $x_vals[$x_index['ICE-SAMPLERATE'][0]]['value'];
                
$this->mount['listeners'] = $x_vals[$x_index['LISTENERS'][0]]['value'];
                
$this->mount['listenurl'] = $x_vals[$x_index['LISTENURL'][0]]['value'];
                
$this->mount['public'] = $x_vals[$x_index['PUBLIC'][0]]['value'];
                
$this->mount['server_description'] = $x_vals[$x_index['SERVER_DESCRIPTION'][0]]['value'];
                
$this->mount['server_name'] = $x_vals[$x_index['SERVER_NAME'][0]]['value'];
                
$this->mount['server_url'] = $x_vals[$x_index['SERVER_URL'][0]]['value'];
                
$this->mount['subtype'] = $x_vals[$x_index['SUBTYPE'][0]]['value'];
                
$this->mount['title'] = $x_vals[$x_index['TITLE'][$xa_count]]['value'];
            }
            else{
                unset(
$this->mount);
                
$this->mount['mount'] = "N/A";
            }
            
$this->version $x_vals[$x_index['SERVER'][0]]['value'];
            
$this->current_listeners $x_vals[$x_index['LISTENERS'][0]]['value'];
        }
        else
            die(
'There is no Server selected. Please select \"shoutcast\" or \"icecast\".');
    }
    function 
listeners(){
        foreach(
$this->x_index['LISTENERS'] as $listener){
            if(
$this->x_vals[$listener]['type'] == "close" || $this->x_vals[$listener 2]['value'] == "\n" || $this->x_vals[$listener 2]['value'] == "")
                break;

            
$this->listener_array .= array(    "hostname" => $this->x_vals[$listener 2]['value'],
                                            
"useragent" => $this->x_vals[$listener 4]['value'],
                                            
"underruns" => $this->x_vals[$listener 6]['value'],
                                            
"connect_time" => $this->x_vals[$listener 8]['value'],
                                            
"pointer" => $this->x_vals[$listener 10]['value'],
                                            
"uid" => $this->x_vals[$listener 12]['value']);
        }
    }
    function 
song_history(){
        
$x_array count($this->x_index['SONG']);
        
$x_array_count 0;

        for(
$x_count 0$x_count $x_array$x_count += 2){
            
$x_song_ $this->x_vals[$this->x_index['SONG'][$x_count] + 2]['value'];
            
$x_song explode(" - "$x_song_);

            
$this->song_history[$x_array_count++] = array("time" => date($this->timeformat$this->x_vals[$this->x_index['SONG'][$x_count] + 1]['value']),
                                                    
"artist" => $x_song[0],
                                                    
"title" => $x_song[1]);
        }
    }
    function 
value($x_nodename){
        return 
$this->x_vals[$this->x_index[$x_nodename][0]]['value'];
    }
    function 
time_format($x_timeformat){
        
$this->timeformat $x_timeformat;
    }
}
?> 
Eigendliche Songabfrage:
song.php
PHP-Code:
<?php
include("radio.class.php");                // Klasse einbinden
$radio = new Radio;                        // Klasse initialisieren

/********** Konfiguration **********/
$radio->set_host("**.**.**.**");            // ServerURL
$radio->set_port("*****");                                // Serverport
$radio->set_pass("********");                            // Passwort für den Adminbereich des Servers
$radio->set_serv("icecast");                            // Legt die Serversoftware fest: "icecast" oder "shoutcast"

$radio->time_format("d.m.Y - H:i:s");                    // Datumsformat festlegen. Beispiele unter http://de2.php.net/manual/de/function.date.php
/*************************************************************/

$radio->init();                                            // Statistik initialisieren

?>
<?php
/********** Schleife, wenn mehrere Mountpoints vorhanden sind und alle gelistet werden sollen **********/
if(is_array($radio->mounts)){                             // Nur ausführen, wenn Mountpoints vorhanden sind, also wenn gesendet wird
    
$m_counter count($radio->mounts);                 // Anzahl der Mountpoints festlegen
    
}
if (  
$radio->mounts[1]['listeners'] > ) { 
    
?>        <div id="titel"><?php echo $radio->mounts[1]['server_name'];?><br /></div>
            <div id="song"><?php echo $radio->mounts[1]['artist'];?> - <?php echo $radio->mounts[1]['title'];?></div>
 <?php
} else {
 
?>            <div id="titel"><?php echo $radio->mounts[0]['server_name'];?><br /></div>
            <div id="song"><?php echo $radio->mounts[0]['artist'];?> - <?php echo $radio->mounts[0]['title'];?></div>
<?php
 
}
?>
Vielen Dank für Eure Hilfe
Netcrash ist offline  
Sponsor Mitteilung
PHP Code Flüsterer

Registriert seit: 21.08.2005
Beiträge: 4682
PHP-Kenntnisse:
Fortgeschritten

Alt 23.03.2010, 11:56  
Erfahrener Benutzer
 
Benutzerbild von Andreas
 
Registriert seit: 22.04.2005
Beiträge: 391
Andreas ist zur Zeit noch ein unbeschriebenes Blatt
Standard

Meinst du sowas? (ungetestet)
PHP-Code:
class radio{
    function 
init(){ // Initialisierung und erzeugung des Arrays mit den Werten
        
$fileCache "./cache.xml";
        if(
file_exists($fileCache) && filemtime($fileCache) > (time()-30)) {
            
$x_xml file_get_contents($fileCache);
        } else {
            
$x_xml $this->get_xml();
            
file_put_contents($fileCache$x_xml);    
        }
        
$this->parse_xml($x_xml);
    } 
Andreas ist offline  
Alt 25.03.2010, 23:01  
Neuer Benutzer
 
Benutzerbild von Netcrash
 
Registriert seit: 23.07.2009
Beiträge: 18
PHP-Kenntnisse:
Anfänger
Netcrash befindet sich auf einem aufstrebenden Ast
Standard

Ich denke das es das schon ist, vielen Dank. Werde es am Wochenende mal testen...
Netcrash ist offline  
Alt 02.04.2010, 22:52  
Neuer Benutzer
 
Benutzerbild von Netcrash
 
Registriert seit: 23.07.2009
Beiträge: 18
PHP-Kenntnisse:
Anfänger
Netcrash befindet sich auf einem aufstrebenden Ast
Standard

Perfekt, vielen Dank das klappt prima
Netcrash ist offline  
 


Themen-Optionen
Thema bewerten
Thema bewerten:

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are an
Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
Problem mit Dateiupload bei eigenem Webserver DarkChris PHP-Fortgeschrittene 12 01.10.2009 13:57
[Erledigt] Eigener Webserver - Header auslesen DarkChris PHP-Fortgeschrittene 5 15.09.2009 15:15
Dateien auf Webserver mit PHP auslesen bluemagicon PHP Tipps 2009 1 31.08.2009 14:40
[Erledigt] bei eigenem CMS Menülinks automatisch auslesen (php + mysql) 18inch PHP-Fortgeschrittene 3 05.01.2005 17:52
[Erledigt] Dateien aus Verzeichnissen vom Webserver auslesen und linken PHP Tipps 2004 3 08.09.2004 10:07

Besucher kamen über folgende Suchanfragen bei Google auf diese Seite
shoutcast listeners and status, icecast auslesen, icecast status, icecast titel auslesen, icecast php, php icecast auslesen, icecast status script, icecast listener, icecast status php, icecast2 php, icecast2 status php, icecast server auslesen, icecast titel auslesen php, php icecast, mit php icecast auslesen, icecast2 status, icecast xml auslesen, shoutcast status auslesen, icecast php title, icecast status auslesen

Alle Zeitangaben in WEZ +2. Es ist jetzt 21:33 Uhr.




Powered by vBulletin® Version 3.7.2 (Deutsch)
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Aprilia-Forum, Aquaristik-Forum, Liebeskummer-Forum, Zierfisch-Forum, Geizkragen-Forum