
Sessantanovesimo articolo dedicato alle web radio.
Ci eravamo lasciati con una panoramica delle funzioni della classe shoutcast. Adesso andiamo a vedere con maggiore precisione come realizzare una pagina php che utilizzi tale classe.
Esaminiamo il file demo.php, a corredo con il pacchetto shoutcast che stiamo utilizzando e che abbiamo trovato al sito phpclasses.org
Il file comincia naturalmente in questo modo:
include(“shoutcast.class.php”);
Questi comandi aprono una pagina php e includono all’interno il file shoutcast.class.php.
Esaminiamo adesso la funzione convertseconds, e vediamo cosa fa:
function ConvertSeconds($seconds) {
$tmpseconds = substr(“00″.$seconds % 60, -2);
if ($seconds > 59) {
if ($seconds > 3599) {
$tmphours = substr(“0″.intval($seconds / 3600), -2);
$tmpminutes = substr(“0″.intval($seconds / 60 – (60 * $tmphours)), -2);
return ($tmphours.”:”.$tmpminutes.”:”.$tmpseconds);
} else {
return (“00:”.substr(“0″.intval($seconds / 60), -2).”:”.$tmpseconds);
}
} else {
return (“00:00:”.$tmpseconds);
}
}
La funzione ConvertSeconds accetta la variabile seconds come unico parametro.
Pone in un altra variabile, chiamata $tmpseconds, una stringa composta da due zeri iniziali e il modulo 60 dei secondi contati dalla variabile seconds, rimuovendo le ultime due cifre (che sono probabilmente i centesimi di secondo).
Se $seconds é maggiore di 59, e contemporaneamente anche maggiore di 3599, allora va settata una variabile $tmphours che conterrá una stringa con il numero di ore calcolate a partire dai secondi e una variabile $tmpminutes, che conterrá una stringa con il numero dei minuti.
Se $seconds é solo maggiore di 59, allora il procedimento per le ore non é piú necessario e quindi si effettua solo il calcolo dei minuti.
Se $seconds non é magiore di 59, si riporta il numero di secondi esattamente come sono.
Passiamo adesso alle dichiarazioni:
$shoutcast = new ShoutCast();
$shoutcast->host = “localhost”;
$shoutcast->port = 8000;
$shoutcast->passwd = “YOURPASSWORDGOESHERE”;
La variabile $shoutcast contiene un nuovo oggetto di tipo shoutcast (poca fantasia, ma meglio non confondere le idee).
Settiamo l’host, la porta e la password del server, che chiaramente, sono dati che voi DOVETE avere a disposizione.
Esaminiamo adesso l’ultima funzione della pagina, la piú importante.
La prima operazione é quella di aprire le statistiche dell’oggetto creato, se l’apertura ha successo, recuperiamo l’xml, e quindi, andiamo a fare operazioni di echo (scrittura) dove decidiamo quando e come visualizzare le informazioni a nostra disposizione. Tutti gli echo, ma chiaramente anche gli altri comandi, sono modificabili a vostro piacimento, in base a come vorrete posizionare le informazioni nella pagina. Le variabili history e listeners sono da considerarsi come possibili array, e quindi é necessario un controllo con la funzione is_array
if ($shoutcast->openstats()) {
// We got the XML, gogogo!..
if ($shoutcast->GetStreamStatus()) {
echo ““.$shoutcast->GetServerTitle().” (“.$shoutcast->GetCurrentListenersCount().” of “.$shoutcast->GetMaxListenersCount().” listeners, peak: “.$shoutcast->GetPeakListenersCount().”)\n\n”;
echo “<table border=”0″ cellspacing=”0″ cellpadding=”0″>\n”;
echo ”
<tbody>
<tr>
<td width=”180″><strong>Server Genre: </strong></td>
<td>”.$shoutcast->GetServerGenre().”</td>
</tr>
\n”;
echo ”
<tr>
<td><strong>Server URL: </strong></td>
<td><a href=”\”>GetServerURL().”\”>”.$shoutcast->GetServerURL().”</a></td>
</tr>
\n”;
echo ”
<tr>
<td><strong>Server Title: </strong></td>
<td>”.$shoutcast->GetServerTitle().”</td>
</tr>
<tr>
<td colspan=”2″></td>
</tr>
\n”;
echo ”
<tr>
<td><strong>Current Song: </strong></td>
<td>”.$shoutcast->GetCurrentSongTitle().”</td>
</tr>
\n”;
echo ”
<tr>
<td><strong>BitRate: </strong></td>
<td>”.$shoutcast->GetBitRate().”</td>
</tr>
<tr>
<td colspan=”2″></td>
</tr>
\n”;
echo ”
<tr>
<td><strong>Average listen time: </strong></td>
<td>”.ConvertSeconds($shoutcast->GetAverageListenTime()).”</td>
</tr>
<tr>
<td colspan=”2″></td>
</tr>
\n”;
echo ”
<tr>
<td><strong>IRC: </strong></td>
<td>”.$shoutcast->GetIRC().”</td>
</tr>
\n”;
echo ”
<tr>
<td><strong>AIM: </strong></td>
<td>”.$shoutcast->GetAIM().”</td>
</tr>
\n”;
echo ”
<tr>
<td><strong>ICQ: </strong></td>
<td>”.$shoutcast->GetICQ().”</td>
</tr>
<tr>
<td colspan=”2″></td>
</tr>
\n”;
echo ”
<tr>
<td><strong>WebHits Count: </strong></td>
<td>”.$shoutcast->GetWebHitsCount().”</td>
</tr>
\n”;
echo ”
<tr>
<td><strong>StreamHits Count: </strong></td>
<td>”.$shoutcast->GetStreamHitsCount().”</td>
</tr>
\n”;
echo “</tbody></table>
“;
echo “<strong>Song history;</strong>
\n”;
$history = $shoutcast->GetSongHistory();
if (is_array($history)) {
for ($i=0;$i echo “[".$history[$i]["playedat"].”] – “.$history[$i]["title"].”
\n”;
}
} else {
echo “No song history available..”;
}
echo “”;
echo “<strong>Listeners;</strong>
\n”;
$listeners = $shoutcast->GetListeners();
if (is_array($listeners)) {
for ($i=0;$i echo “[".$listeners[$i]["uid"].”] – “.$listeners[$i]["hostname"].” using “.$listeners[$i]["useragent"].”, connected for “.ConvertSeconds($listeners[$i]["connecttime"]).”
\n”;
}
} else {
echo “Noone listens right now..”;
}
} else {
echo “Server is up, but no stream available..”;
}
} else {
// Ohhh, damnit..
echo $shoutcast->geterror();
}
?>
Per qualsiasi dubbio, commentate.





