I’ve been experimenting with the jQuery Javascript library, and was looking for a little project to try it out on. So I made a test page at http://yggdrasilradio.net/demo/ that polls my Shoutcast server data every 15 seconds using a PHP script, parses the XML that comes back, and displays it in a table using AJAX to update the page.
The book I’m reading to learn from is Learning jQuery, and it’s pretty good, although the way the book describes AJAX (”Make your website buzzword-compliant!”), while amusing, gives me the feeling the author really isn’t taking this particular subject all that seriously.
If you have a Shoutcast server and want to play with the code yourself, have at it: demo.zip. Unzip that and put the “demo” folder just off your webroot on a webserver, with all the files in it. There’s a couple of parameters at the beginning of playing.php you’ll need to edit for your Shoutcast server.
Start out by loading playing.php from your webserver into your browser. If you see a bunch of song titles, you’ll know all is well. If your webserver doesn’t support PHP or the PHP Curl library used to access the Shoutcast XML data, then you’ll be out of luck. Take a look at the source of the returned page to see what it really looks like, since your webbrowser will mush it all together.
Then load up the main page at http://yourserver.com/demo. You should see a pretty display of your listeners, the currently playing song, and the five previously played songs. I might make this into a nice plugin for Wordpress one of these days.
Let the page sit there for awhile, and you’ll notice that it updates itself automatically as new songs play, listeners join or leave the audio stream, etc. That’s what AJAX is doing for you. The PHP script even attempts to figure out where all your listeners are coming from, though it’s a bit simple-minded: it thinks anyone from a .com, .mil, .net or .org domain is from the USA.
Since each copy of the page that’s running anywhere in the world will hit the Shoutcast server for data every 15 seconds, this technique might not be good for Shoutcast streams with 300 listeners, but it works okay for my station. You can find the full non-jQuery version of my streaming radio station at http://yggdrasilratio.net.
You must be logged in to post a comment.
Comments
This entry was posted on Tuesday, August 19th, 2008 at 10:02 pm and is filed under Workshop. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.
Category:

hi,
Good script but I have a error :
Warning: Invalid argument supplied for foreach() in /home/crash/public_html/demo/playing.php on line 263
Thank you.
I couldn’t duplicate that particular failure, but I did find a bug, which I fixed. Try downloading it from scratch and trying again — I made a slight change to playing.php. Also, I wonder if your Shoutcast server is configured to give raw IP addresses for your listeners rather than domain names, and if that would cause that.
I get the same error on line 263. Can’t figure out what’s wrong. Will try to fix it myself and let you know if I find anything.
Thanks, I really appreciate that. I’ll take another look myself tonight. I have no idea what’s going on.
Yah, the problem was that the demo.php had the $server variable by default at $server=http:// so deleting that fixed everything. $server must be IP with port no http://.
You rock! Thank you, I’ll see if I can fix that tonight and reupload so it doesn’t bite someone else.
There’s also a php class available you might be interested in. It’s a really complete class with all the error handling.
http://webscripts.softpedia.com/script/PHP-Clases/shoutcast-class-php-12506.html
Thanks a lot for this. I have an immediate use for it. Will show results once it’s live.
Wondering if there’s a way to show jquery effects when tracks actually update (or when there?s an actual change in the XML loaded). This is beyond my knowledge.
I do have an issue when “&” characters appear in tracktitles. I dunno but it seems to add a carriage return and splits what should be in 1 line into 3.
Example:
Bob & Jones - Shout
will turn into
Bob
&
Jones - Shout
Fixed ampersand issue by adding the following lines:
$xml = str_replace(”&”, “and”, $xml);
$xml = str_replace(”#x26;”, “”, $xml);
Here is the result http://www.radionula.com/
Thank you!
Nice job! Glad I could help!
I tried your code and is excellent but i cant get to make the country stuff work, do you have any tips?
One thing that would make the country lookup fail is that the “Name Lookups” is set to “off” in your Shoutcast server settings on your Shoutcast server host. That’d mean that your listeners IPs would show up in the form 192.128.113.100, rather than looking like nameoflistenerisp.com, etc.
So check on that. Another way to check if that’s what’s going on is to log into the admin page of your Shoutcast server and when you get to the page that says “SHOUTcast Listeners and Status” there’ll be a little Get XML Stats: [Yes] link… click that and see what form that’s in.
On mine the listener HOSTNAMEs are listed as a130-180.csie.chu.edu.tw etc.
hi !
Thanks for this great script, have only question about IE and Opera… automatic update of songs don’t work in ie and opera… tryed
with changing setinterval and others but not working… last chance was in php
and then work index.html via ajax update… but that’s poor, because of requesting every 10 sec to server..
tryed
meta http-equiv=”Refresh” content=”10; url=playing.php”
Right, forget that “refresh” tag, that’ll drive people nuts.
There’s a line in index.html that looks like:
url: ‘playing.php’,
Change that to look like:
url: ‘playing.php?id=’+Math.random(),
That ought to do it.
I really need to come up with a new version of this script.
hi there! im having this error
Warning: Cannot modify header information - headers already sent by (output started at C:\www\demo\playing.php:1) in C:\www\demo\playing.php on line 241
Currently Playing
Previous Songs
Warning: Invalid argument supplied for foreach() in C:\www\demo\playing.php on line 266
I dont have the current song display. help please? thank you!
Already fixed the errors above..i added
$xml = str_replace(”&”, “and”, $xml);
$xml = str_replace(”#x26;”, “”, $xml);
but it says undefined.
I see slanted quote marks on those replace statements. Not sure they’re actually in the file or not, but edit the file with notepad.exe and make sure the quotes look like " and not like “ or ”
I’ll take a look at this tonight.
One problem is that you may have a blank line before the first <?php in the file, or you may have added an extra line of code somewhere before the line that says “header(”Content-Type: text/html”);”
I guess you can move that “header(”Content-Type: text/html”);” line to just after that initial <?php line, and make sure there’s no blank line before that one.
You’ve got other problems; I’m still looking at it, but that might be a start.
thanks rick! i just solved my problem. i have another question on how to put scroll in last songs played ?
Send me an email so I can ask you where you’ve got the station located.
Hi Rick - is it possible to hack this script to give just the listener count, from several servers
Maybe something like http://yggdrasilradio.net/download/demo3.zip
Demo at http://yggdrasilradio.net/demo3
Replace your own server URL in the script, of course.
Its quite a handy script Rick thx! There is one thing though I don’t know. That is..
How to pass values of the variables currentsong, prevsong1,prevsong2.. to php?
Can you tell me the tricks? thx
There’s a Javascript script in the index.html file that calls the playing.php script to plug in all the values.
Any support for this still ??
Got this error
Listeners
Warning: Cannot modify header information - headers already sent by (output started at /home/innerba1/public_html/demo/playing.php:1) in /home/innerba1/public_html/demo/playing.php on line 241
Currently Playing
Previous Songs
Warning: Invalid argument supplied for foreach() in /home/innerba1/public_html/demo/playing.php on line 265
Did try the fix by dj but made no difference.
Try downloading it again… I changed playing.php to put the header line in as soon as possible. Also make sure there isn’t a blank line before the first <php in the playing.php file. Also make sure you have “Name Lookups” turned on for your Shoutcast server.
Heya i ahve tried both the full and the just the listners php file but when the page is loading and that bit loads it causes all the site to screw up and fly everywhere http://rollercoastermad.com/radio/new/
I have been commenting bit out and worked out its when the main script on both of them start
$(document).ready(function() {
$(’div’).css(”display”, “inline”);
pollstation();
setInterval(pollstation, 15000);
});
function pollstation() {
$.ajax( {
url: ‘listeners.php’,
type: ‘GET’,
dataType: ‘html’,
success: function(stationdata) {
$(’#listeners’).html(stationdata);
}
} );
}
so is there anyway to fix this?