Code - steamcaststream
/* Steamcast Class to pull info about your stream and pass it off to other pages
/* Author: Jay Krivanek (http://www.radiotoolbox.com and http://www.steamcast.com)
/* License: Free to use and distribute for anyone any where.
/* No warranty implied or offered for the code below. Use at your own risk!
/* So I know you are in a super huge rush to use this class and dig in. First a few things about the class.
/* I literally wrote this code in an hour. So there might be bugs, but hey, it's free!
/* You use this class to access either all streams on a server or one. If you need only one, then please specify so in the
/* $steamcast_url of the constructor. If you want all your streams then just pass the base url to the constructor.
/* // In this example we just want one stream's info, that stream would be whatever is at mount /live
/* $steamcast = new SteamcastStream('http://localhost:8000/live', 'admin', 'mysecretpassword');
/* // In this example we want all of the streams that localhost:8000 has
/* $steamcast = new SteamcastStream('http://localhost:8000/', 'admin', 'mysecretpassword');
/* You can also pass authentication details and the address in one single argument like so
/* $steamcast = new SteamcastStream('http://admin:[email protected]:8000/live');
/* Any credentials passed this way will override the optional user and pass credentials.
/* Once this is done you will need to retrieve the stats by calling $steamcast->retrieve_stats()
/* If this call succeeds it will return true, false will be returned if something is wrong.
/* You also might want to check if the server has streams by checking $steamcast->has_streams()
/* This will return true if it does, false if not. If this returns false then the only valid call you can make is
/* $steamcast->to_array() which will return an array with some general information about the server state.
/* This can happen if the server has no mounts created or an invalid mount url was passed to the constructor.
/* You can get the details of a particular stream by calling $steamcast->stream_info(). If more than one mount is expected
/* You can pass the index into this function $steamcast->stream_info(2), use $steamcast->stream_count() to ensure that '2' would have
/* valid data in it.
private $url = array();
// Users of this class should construct the object with the base url of their steamcast server and
// authentication details for statistics
public function __construct($steamcast_url, $username = '', $password = '')
$this->user = $username;
$this->pass = $password;
$this->url = parse_url($steamcast_url);
if (!isset($this->url['host']) && !isset($this->url['scheme']) && isset($this->url['path']))
$this->url = parse_url('http://'.$this->url['path']);
$this->user = $this->url['user'];
$this->pass = $this->url['pass'];
$this->url['port'] = 80;
$this->url['path'] = '/';
// This function actually is required to retrieve the stats.
public function retrieve_stats()
$opts = array(
'header'=>"User-Agent: Steamcast Stats (compatible; Mozilla/5.0)\r\n" .
"Authorization: Basic ".base64_encode($this->user.':'.$this->pass)."\r\n"
$context = stream_context_create($opts);
$query = '';
if ($this->url['path'] != '/')
$query = '?src='.urlencode($this->url['path']);
$json = file_get_contents('http://'.$this->url['host'].':'.$this->url['port'].'/admin/status.json'.$query, false, $context);
$array = json_decode($json,TRUE);
$this->data = $array;
// returns the count of stream nodes in the array.
public function stream_count()
// tells us if we have any source nodes in the sheet we retrieved.
public function has_streams()
return isset($this->data['sources']) && count($this->data['sources']) > 0;
// Gives us access to all resources we successfully retrieved.
public function to_array()
// Returns just data for the stream we want by index.
public function stream_info($index = 0)
if (count($this->data['sources']) > $index)
//This is an example
$steamcast = new SteamcastStream('mysteamcastserver.com', 'admin', 'mysecretpassword');
if ($steamcast->retrieve_stats() && $steamcast->has_streams())
$data = $steamcast->stream_info();
if ($data != NULL)
echo 'Wow, '.$data['name'].' has some shtuff in it! Currently there are '.$data['nodes'].' listeners! They are currently hearing '.$data['meta_song'].'';
while (list($key, $val) = each($data['played']))
echo '<br> at '.date('r', $val['entry_time']).' we played '.$val['song_title'];
while (list($key, $val) = each($data['peers']))
echo '<br> '.$val['ip'].' connected at '.date('r', $val['pconnect_time']).' with '.$val['user_agent'];
echo 'No Mount by this name!';
echo "This failed :(";