SteamcastServer.php - Class for retrieving XML stats

Steamcast is a stand alone server that combines the features of SHOUTcast and Icecast2 and more to make one mega awesome server.
Post Reply
shoebox
Posts: 8
Joined: Thu May 08, 2003 6:19 am
Location: Wichita falls Tx
Contact:

SteamcastServer.php - Class for retrieving XML stats

Post by shoebox » Wed Feb 14, 2007 8:22 am

1.) Save this code to file - SteamcastServer.php.

Code: Select all


<?php

class SteamcastServer 
{

	// Public
	var $host;
	var $port;

	//Private
	var $_xml;
	var $_error;
	var	$_currentListeners;
	var	$_streamStatus;
	var	$_peakListeners;
	var	$_maxListeners;
	var	$_reportedListeners;
	var	$_bitrate;


	function SteamcastServer($host, $port)
	{
		$this->host = $host;
		$this->port = $port;
	}

	function retrieveStats() {
		$page = null;
		$connected = 0;
		$fp = @fsockopen($this->host, $this->port, $errno, $errstr, 1);


		if (!$fp) {
			$this->_error = "$errstr ($errno) - $this->port";
			return(0);

		} else { 
			
			// grab XML stats
			stream_set_timeout($fp, 2);
			$status = null;
			$status = socket_get_status($fp);

			fputs($fp, "GET /status.xml HTTP/1.0\r\n");
		    fputs($fp, "User-Agent: Mozilla\r\n\r\n");


		    while (!feof($fp) && !$status['timed_out']) {
				$line = trim(fgets($fp, 512));
				$this->_xml .= (empty($line)) ? null : $line;
				$status = socket_get_status($fp);
		    }

			$this->_xml = preg_replace("/^HTTP(.*?)xml/", "", $this->_xml);
			
		    fclose($fp);


			$xmlparser = xml_parser_create();
			if (!xml_parse_into_struct($xmlparser, $this->_xml, $this->_values, $this->_indexes)) {
				$this->_error = "Unparsable XML";
				echo $this->_xml;
				return(0);
			}
	
			xml_parser_free($xmlparser);
			//print_r($this->_indexes);
			//print_r($this->_values);
			//die();
			return(1);
		}


	}

	function getCurrentListenersCount() {
		return ($this->_currentListeners) ? $this->_currentListeners : $this->_values[$this->_indexes["TOTAL_NODES"][0]]["value"];
	}

	function getPeakListenersCount() {
		return ($this->_peakListeners) ? $this->_peakListeners : $this->_values[$this->_indexes["TOTAL_PEAK_NODES"][0]]["value"];
	}

	function getMaxListenersCount() {
		return ($this->_maxListeners) ? $this->_maxListeners : $this->_values[$this->_indexes["TOTAL_MAX_NODES"][0]]["value"];
	}
	
	function getUniqueListenersCount() {
		return ($this->_reportedListeners) ? $this->_reportedListeners : $this->_values[$this->_indexes["TOTAL_UNIQUE_NODES"][0]]["value"];
	}
	
	function getAverageListenTime() {
		return($this->_values[$this->_indexes["TOTAL_AVERAGE_CONNECT_TIME"][0]]["value"]);
	}
	
	function getServerGenre() {
		return($this->_values[$this->_indexes["GENRE"][0]]["value"]);
	}
	
	function getURL() {
		return($this->_values[$this->_indexes["URL"][0]]["value"]);
	}
	
	function getServerTitle() {
		return($this->_values[$this->_indexes["NAME"][0]]["value"]);
	}
	
	function getCurrentSongTitle() {
		return($this->_values[$this->_indexes["META_SONG"][0]]["value"]);
	}
	

	function getStreamHitsCount() {
		return($this->_values[$this->_indexes["TOTAL_TUNEINS"][0]]["value"]);
	}
	
	function getStreamStatus() {
		return (isset($this->_streamStatus)) ? $this->_streamStatus : $this->_values[$this->_indexes["STATUS"][0]]["value"];
	}

	function getWebHitsCount() {
		return($this->_values[$this->_indexes["TOTAL_TUNEINS"][0]]["value"]);
	}
	
	function getBitRate() {
		return (isset($this->_bitrate)) ? round($this->_bitrate) : round($this->_values[$this->_indexes["BITRATE"][0]]["value"]);
	}
	
	
	function getError() { return($this->_error); }
	function getHost() { return($this->host); }
	function getPort() { return($this->port); }


}

?>


2.) Usage:

Code: Select all

<?php
include('SteamcastServer.php');

$Server = new SteamcastServer(
	$host = 'master.dnbradio.com',
	$port = 8020,
	$password = null);

$Server->retrieveStats();

echo '<br />Server Title: '.$Server->getServerTitle();
echo '<br />Stream Status: '.$Server->getStreamStatus();
echo '<br />Server Genre: '.$Server->getServerGenre();
echo '<br />Server URL: '.$Server->getURL();
echo '<br />Song Title: '.$Server->getCurrentSongTitle();
echo '<br />Bitrate: '.$Server->getBitRate();
echo '<br />Listeners:'.$Server->getCurrentListenersCount();
echo '<br />Max Allowed: '.$Server->getMaxListenersCount();
echo '<br />Listeners Peak: '.$Server->getPeakListenersCount();
echo '<br />Listeners Unique: '.$Server->getUniqueListenersCount();
echo '<br />Stream Hits: '.$Server->getStreamHitsCount();
echo '<br />Web Hits: '.$Server->getWebHitsCount();
echo '<br />Average Listen Time: '.$Server->getAverageListenTime();
?>

Here's a sample of it in action: - http://www.dnbradio.com/baselib/models/ ... rver_t.php

I use this class and another one (ShoutcastServer.php) on my site's server list page (the 56k, 32k, and 24k servers at the bottom of the list are steamcast, the rest are shoutcast).
- http://www.dnbradio.com/servers

User avatar
Jay
Will work for food (Administrator)
Posts: 2862
Joined: Mon Jan 14, 2002 12:48 am
Location: Next Door
Contact:

Post by Jay » Wed Feb 14, 2007 4:34 pm

Good stuff, thanks for your contribution.
- Jay

Post Reply