help with metadata in PHP, with shoutcast v2

nelson c

Well-known member
Hello, anyone has an idea how to modify this code to work in shoutcast 2?
Apparently "@ fsockopen" expects a host and port but does not acepted the Sid shoutcast 2.
Any information is appreciated!
Code:
<? header("Content-Type: text/plain; charset=ISO-8859-1"); ?> 
<html><body leftmargin="5" topmargin="5" marginwidth="0" marginheight="0">
<div align="Left">
<strong> 
<?php
$host = "http://s1.radioboss.fm"; // ip or url of shoutcast server
$port = "9036";          // port of shoutcast server 
$fp = @fsockopen("$host", $port, $errno, $errstr, 30);

if($fp)
{
  fputs($fp,"GET /7.html HTTP/1.0\r\nUser-Agent: GET SEVEN (Mozilla Compatible)\r\n\r\n");

  while(!feof($fp))
  {
    $data .= fgets($fp, 1000);
  }

  fclose($fp);

  $data              = ereg_replace(".*<body>", "", $data);
  $data              = ereg_replace("</body>.*", ",", $data);
  $data_array        = explode(",",$data);
  $listeners         = $data_array[0];
  $status            = $data_array[1];
  $peak_listeners    = $data_array[2];
  $maximum_listeners = $data_array[3];
  $unique_listeners  = $data_array[4];
  $bitrate           = $data_array[5];
  $track             = $data_array[6];
}

$title  = chop($track);
$select = explode(" - ",$title);
$artist = chop($select[0]);
$title  = chop($select[1]);

if($status == 1)
{


//...title only
//print 'document.getElementById("title").innerHTML = "' . $title . '";';
}


echo "<p> <font color=Black font face='Arial' size='4pt'>$artist</br>";
echo "<p> <font color=Black font face='Arial' size='2.5pt'>$title</p>";?>
</strong></font>
</div>
</body>
</html>
 
I think Shoutcast v2 has a different approach.

To get stats in XML, use this URL:
Code:
http://s1.radioboss.fm:9036/stats?sid=1

In PHP you don't need to bother with fsockopen, there's easier, one-line solution:
Code:
$s = file_get_contents("http://s1.radioboss.fm:9036/stats?sid=1")
$s will have XML response which contains listener count, titles and other information.

You can then parse XML using SimpleXML http://www.php.net/manual/en/simplexml.examples-basic.php or just do a string search.
 
Dmitry, thank you very much for the help :D!, Did not know that worked with xml Shoutcast 2.
I was all morning proving several codes that use "file_get_contents" but only one I worked, the problem is that despite having encoding "utf-8", has problems with some characters ('n', ',', &. ..). any idea what can be?
Code:
<html><body leftmargin="5" topmargin="5" marginwidth="0" marginheight="0">
<div align="Left">
<strong> 
<?php
header("Content-Type: text/html;charset=utf-8");
$cc=10;

$url = "http://s1.radioboss.fm:9036/stats?sid=1.xml";

//ELEMENTS TO RETRIEVE - YOU MUST KNOW THE TAG NAMES OF EACH ELEMENT - Can be improved using an array (next update):
$title='';
$author='';
$date='';
$article='';
$link='';

//Counter to start counting the number of items
$cz=0;

$data = file_get_contents($url);
$depth = array();

//The dataset will be stored inside this variable
$txt='';

function startElement($parser, $name, $attrs)
{
	//we use global variables to keep track of each element while parsing the whole document
	global $cc,$cz,$txt;
    global $depth;
    global $tagname;
	global $title,$author,$date,$article,$link;

	if($cz < $cc)
	{
		/* display only if we find all items to display - updated here*/
		if(strlen(trim($article)) > 0  && strlen(trim($title)) > 0 && strlen(trim($author)) > 0 && strlen(trim($date)) > 0)
		{
			//at this level we have a NEW ITEM -> we increment to items' counter
			if($cz++ >= 1)
			{
				/*We display the dataset in the order we wish (see below to apply a different design)*/
				$txt.=$title.$author.$date.$article;
				$title='';
				$author='';
				$date='';
				$article='';
			}
		}

		$tagname=$name;
	}

	//one level deeper
	$depth[$parser]++;
}

function endElement($parser, $name)
{
    global $depth;
    global $tagname;
	$tagname='';

    $depth[$parser]--;

}

function characterData($parser, $ddata)
{
    global $tagname;
	global $cc,$cz,$txt;
	global $title,$author,$date,$article,$link;

	/*For each tag name we receive we only extract the wanted ones
	You can apply different layout to the data
	*/
	if($cz < $cc)
	{
		switch($tagname)
		{
			case 'SONGTITLE':if(strlen(trim($ddata)) > 0) $title= utf8_encode($ddata);break;
			
			default: break;
			
			case 'SONGTITLE':if(strlen(trim($ddata)) > 0) $title= utf8_encode($ddata);break;
			
		
		}
	}

}

/*everything is triggered here - we parse the xml file and call the startElement & endElement functions, then the characterData function to read all content - the xml_parse line launches the procedure taking care of the arguments*/
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

if (!xml_parse($xml_parser, $data))
die(sprintf("XML error: %s at line %d",	xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser)));

xml_parser_free($xml_parser);

//We store the last elements
$txt.=$title.$author.$date.$article;


/*echo the result inside a table (you may write it into a file)*/
$txt= '<i>'.$txt.'</i>';
$select = explode(" - ",$txt);
$artist = chop($select[0]);
$title  = chop($select[1]);

echo "<p> <font color=Black font face='Arial' size='4pt'>$artist</br>";
echo "<p> <font color=Black font face='Arial' size='2.5pt'>$title</p>";?>

</strong></font>
</div>
</body>
</html>
 
If the problem is that it inserts a backslash (\) before those symbols, I think this is the solution: http://www.djsoft.net/smf/index.php/topic,3195.msg11328.html#msg11328
 
djsoft said:
I think Shoutcast v2 has a different approach.

To get stats in XML, use this URL:
Code:
http://s1.radioboss.fm:9036/stats?sid=1

In PHP you don't need to bother with fsockopen, there's easier, one-line solution:
Code:
$s = file_get_contents("http://s1.radioboss.fm:9036/stats?sid=1")

$s will have XML response which contains listener count, titles and other information.

You can then parse XML using SimpleXML http://www.php.net/manual/en/simplexml.examples-basic.php or just do a string search.

Thanks Dmitry, you was right.
Do some research and I could easily create my own code:
Here I share it in case someone needs it (since there are several Internet for Shoutcast V2 codes that do not work)

Code:
<html><body leftmargin="5" topmargin="5" marginwidth="0" marginheight="0">
<div align="Left">
<strong> 
<?php
$url = file_get_contents("http://s1.radioboss.fm:9036/stats?sid=1.xml");
$xml = simplexml_load_string($url);
foreach ($xml->SONGTITLE as $cancion)
$cancion = utf8_decode($cancion);

$select = explode(" - ",$cancion);
$artista = chop($select[0]);
$titulo  = chop($select[1]);

echo "<p> <font color=Black font face='Arial' size='4pt'>$artista</br>";
echo "<p> <font color=Black font face='Arial' size='2.5pt'>$titulo</p>";?>
</strong></font>
</div>
</body>
</html>
 
Back
Top