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
Eigendliche Songabfrage:
song.php
Vielen Dank für Eure Hilfe
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, &$errstr, 10);
if (!$sp)
die('Error while connecting to Server.');
else{
set_socket_blocking($sp, false);
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($sp, 31337);
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;
}
}
?>
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'] > 0 ) {
?> <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
}
?>
Kommentar