Displaying Now Playing Info from Shoutcast Stream on Wordpress website

572 views Asked by At

I'm looking for a script that I can easily implement into my Wordpress based website that will show the 'Now Playing' information from my online radio station.

All that I am looking for is a simple text output, whereas most providers offer scripts that of a fixed size with images etc.

The stream status page is http://radio.streemlion.com:2780/status.xsl

I'm quite new to coding so please forgive my basics - I've seen some examples where you have to implement scripts and/or pages which I can't easily do within a wordpress based site.

Thanks in advance!

1

There are 1 answers

6
Snuffy On

Like its mention best approach is using API but you can use curl too to grab content. Keep in mind this is not the best solution for this since it will be slow.

Place following function in your functions.php then call that function wherever you want.

function current_song_name() {
    $ch = curl_init();
 
// URL for Scraping
curl_setopt($ch, CURLOPT_URL,'http://radio.streemlion.com:2780/status.xsl');
 
// Return Transfer True
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($output);
$xpath = new DomXPath($dom);
$inputs = $dom->getElementsByTagName('td');
$data = array();
foreach($inputs as $input):
    $data[] = $input->textContent;
endforeach;
echo 'Currently playing: '.end($data);

// Closing cURL
curl_close($ch);

}

Update

You can make the following function into shortcode like this

add_shortcode( 'csn', 'current_song_name' ); // where csn is our shortcode

Either use in your content [csn] or in php echo do_shortcode('[csn]');

Read more about it - https://codex.wordpress.org/Shortcode_API https://developer.wordpress.org/reference/functions/do_shortcode/