Comunidad PHPeros

Lenguajes => PHP => Tutoriales PHP => Mensaje iniciado por: naveda en 08 de Noviembre de 2006, 21:53:58 pm

Título: Radio Stats - Codigo y Explicación
Publicado por: naveda en 08 de Noviembre de 2006, 21:53:58 pm
Hola PHPero,
En este tutorial vas a aprender el funcionamiento del sistema de estadisticas de una radio online.
El codigo fue creado por ©Daniel Brown y ahora vamos a explicar todo el codigo.

El sistema original se compone de 5 archivos:

De estos 5 archivos explicaremos los necesarios, que son:

Antes de comenzar vamos a comentar las caracteristicas basicas de estos dos archivos, el config_radio.php es simplemente un archivo con los datos necesarios para establecer conexion con el servidor de la radio de donde se extraerán los datos de las estadisticas y el radio_stats.php realiza los procesos necesarios para extraer los datos de las estadisticas y mostrarlos.

<?php
//////////////////////////////////////////////////////////////////////////////////////////////
// Dinghy Radio Stats Information
// Grabs the xml from your radio server and displays it in a iframe that refreshes every 30 seconds.
// ©Daniel Brown www.gmtt.co.uk
//////////////////////////////////////////////////////////////////////////////////////////////
//Configuracion
$scdef = "Nombre de la Radio";               // Nombre de la radio por defecto, se muestra cuando el servidor esta caido
$scip = "IP"; // ip or url del servidor shoutcast (NO AÑADIR HTTP:// No incluir el puerto)
$scport = "PUERTO";                   // Puerto del servidor shoutcast
$scpass = "PASSWORD";                 // Contraseña del servidor to shoutcast
//Fin configuration
?>

Como se puede apreciar este archivo solamente sirve para definir los datos de conexion al servidor shoutcast, que son:

<?PHP
// Shoutcast Server Stats
// Parses shoutcasts xml to make an effective stats thing for any website
// ©2004-2005 Daniel Brown http://www.gmtt.co.uk
// Please refer to the readme file for use.
include('config_radio.php');  //you may edit this path to fit your server environment otherwise leave it alone
$scfp = fsockopen("$scip", $scport, &$errno, &$errstr, 30);
 if(!$scfp) {
  $scsuccs=1;
echo''.$scdef.' Actualmente, no hay ningún DJ paciencia porfavor ;)';
 }
if($scsuccs!=1){
 fputs($scfp,"GET /admin.cgi?pass=$scpass&mode=viewxml HTTP/1.0\r\nUser-Agent: SHOUTcast Song Status (Mozilla Compatible)\r\n\r\n");
 while(!feof($scfp)) {
  $page .= fgets($scfp, 1000);
 }
########################################################
/////////////////////////part 1 \\\\\\\\\\\\\\\\\\\\\\\\
//define  xml elements
 $loop = array("STREAMSTATUS", "BITRATE", "SERVERTITLE", "CURRENTLISTENERS");
 $y=0;
 while($loop[$y]!=''){
  $pageed = ereg_replace(".*<$loop[$y]>", "", $page);
  $scphp = strtolower($loop[$y]);
  $$scphp = ereg_replace("</$loop[$y]>.*", "", $pageed);
  if($loop[$y]==SERVERGENRE || $loop[$y]==SERVERTITLE || $loop[$y]==SONGTITLE || $loop[$y]==SERVERTITLE)
   $$scphp = urldecode($$scphp);

// uncomment the next line to see all variables
//echo'$'.$scphp.' = '.$$scphp.'<br>';
  $y++;
 }
//end intro xml elements
########################################################
########################################################
/////////////////////////part 2\\\\\\\\\\\\\\\\\\\\\\\\\
//get song info and history
 $pageed = ereg_replace(".*<SONGHISTORY>", "", $page);
 $pageed = ereg_replace("<SONGHISTORY>.*", "", $pageed);
 $songatime = explode("<SONG>", $pageed);
 $r=1;
 while($songatime[$r]!=""){
  $t=$r-1;
  $playedat[$t] = ereg_replace(".*<PLAYEDAT>", "", $songatime[$r]);
  $playedat[$t] = ereg_replace("</PLAYEDAT>.*", "", $playedat[$t]);
  $song[$t] = ereg_replace(".*<TITLE>", "", $songatime[$r]);
  $song[$t] = ereg_replace("</TITLE>.*", "", $song[$t]);
  $song[$t] = urldecode($song[$t]);
  $dj[$t] = ereg_replace(".*<SERVERTITLE>", "", $page);
  $dj[$t] = ereg_replace("</SERVERTITLE>.*", "", $pageed);
$r++;
 }
//end song info

fclose($scfp);
}

//display stats
if($streamstatus == "1"){
//you may edit the html below, make sure to keep variable intact
echo'
<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<META HTTP-EQUIV="REFRESH" CONTENT="30;URL=radio_stats.php">
<link rel=stylesheet href="" type="text/css">
<title>'.$scdef.'</title>
</head>

<body text="" bgcolor="">




<p align="center"><center>

<font face="arial" size="2"><b>&nbsp;Server Name:</b>&nbsp;'.$servertitle.'</font></p>
<font face="arial" size="2"><b>&nbsp;Listeners:</b>&nbsp;'.$currentlisteners.' / 10</font></p>

<font face="arial" size="2" color=""><b>

    Current Song:</b> '.$song[0].'</font></p>
    <b>
 

<font face="arial" size="2">
    Past Songs:</font></b>
<font color=""><font face="arial" size="2">
<p align="center">
<b>1.</b>  '.$song[1].'<BR>
<b>2.</b>  '.$song[2].'<BR>
<b>3.</b>  '.$song[3].'<BR>
<b>4.</b>  '.$song[4].'<BR>
<b>5.</b>  '.$song[5].'<BR>
</font>
<BR>
</p></p>
</body>

</html>';
}
if($streamstatus == "0")
{
//you may edit the html below, make sure to keep variable intact
echo'
<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<META HTTP-EQUIV="REFRESH" CONTENT="30;URL=radio_stats.php">
<link rel=stylesheet href="" type="text/css">
<title>Radio Server Is Offline</title>
</head>

<body text="" bgcolor="">
Server Offline!
</body>

</html>';
}
?>

En este archivo la explicación es más extensa y necesita una explicación mas detenida y concreta.
En principio este archivo realiza los siguientes procesos:

LA EXPLICACIÓN NO ESTA TERMINADA, PRONTO LA ACABARÉ