Web / PHP

PHP class for Shoutcast V2, how to get current song playing

by Libz
28 Jul 2017 at 11:47hrs | 4082 Views
I have been working on an online radio station that uses Shoutcast servers for streaming, by the way www.shoutcast.com offers free streaming.

Here is a PHP class I created for generating:

  1. Current song playing
  2. DJ Name
  3. Show name
  4. Show time
  5. Listen URL
This class works with Shoutcast v2, stream via www.shoucast.com

In order to access the Shoutcast API your will need your Shoutcast.com login details: email and password

Shoutcast API (XML) - here you have to replace [MOUNT] with your radio station mount, which is found under streams when logged into your Shoutcast account. Also you have to replace [EMAIL] with the login email and [PASSWORD] with your login password. These details will be in a PHP file, thus they wont be seen by the public.

This tool (Radionomy Aggregator) will generate an XML file with your server details.
$xml_url = "http://aggregator.radionomy.com/?mount=/[MOUNT]&user=[EMIAL]&password=[PASSWORD]";

The PHP class that we are going to create will loop through XML ($xml_url) and look for the source element.

If you are using WinAmp you will notice that there is nowhere to fill in the name of current DJ, Show name and Show time. These details can be put in the  Server Name field ordered as follows: Server name - Show Name - Show Time (e.g 1pm-2pm) - DJ name

Please note, the order and the gap before and after the dash that separates these details.

Here is the Class:

class Radio
public $current_playing;
public $dj_name;
public $show_time;
public $show_name;
public $stream_url;
public $radio_stats;
private $server_name;
public $listenurl;

function __construct($stream_url){
$stream_xml = simplexml_load_file($stream_url);
$this->radio_stats = $stream_xml->source;
$server_name = $this->radio_stats->server_name;

$server_name = explode(' - ', $server_name);

$this->now_playing = $this->radio_stats->yp_currently_playing;

$this->show_name = $server_name[1];
$this->show_name = 'Show name not set';

$this->show_time = $server_name[2];
$this->show_time = 'Show time not set';

$this->dj_name = $server_name[3];
$this->dj_name = 'Auto DJ';

$this->listenurl = $this->radio_stats->listenurl;


How to use the class

$radio = new Radio($xml_url);

//Get title of song currently playing
echo $radio->now_playing;

//Get the name of the show
echo $radio->show_name;

//Get the show time
echo $radio->show_time;

//Get the name of current DJ
echo $radio->dj_name;

If you have any questions just live a comment below.

Source - AC

Chat Login

Register | Forgot password