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.

28 Comments so far

  1. crash on December 8, 2008 3:09 am

    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.

  2. rickadams on December 8, 2008 8:17 pm

    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.

  3. ag211 on February 5, 2009 7:15 am

    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.

  4. rickadams on February 5, 2009 8:10 am

    Thanks, I really appreciate that. I’ll take another look myself tonight. I have no idea what’s going on.

  5. ag211 on February 5, 2009 8:11 am

    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://.

  6. rickadams on February 5, 2009 8:19 am

    You rock! Thank you, I’ll see if I can fix that tonight and reupload so it doesn’t bite someone else.

  7. ag211 on February 5, 2009 1:52 pm

    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

  8. ag211 on February 6, 2009 7:29 am

    Fixed ampersand issue by adding the following lines:

    $xml = str_replace(”&”, “and”, $xml);
    $xml = str_replace(”#x26;”, “”, $xml);

  9. ag211 on February 6, 2009 1:20 pm

    Here is the result http://www.radionula.com/
    Thank you!

  10. rickadams on February 6, 2009 11:56 pm

    Nice job! Glad I could help!

  11. laoman on March 25, 2009 10:37 pm

    I tried your code and is excellent but i cant get to make the country stuff work, do you have any tips?

  12. rickadams on March 26, 2009 9:15 pm

    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.

  13. Oluj@ on May 18, 2009 8:07 am

    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..

  14. Oluj@ on May 18, 2009 8:07 am

    tryed
    meta http-equiv=”Refresh” content=”10; url=playing.php”

  15. rickadams on May 18, 2009 6:39 pm

    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.

  16. djcomrad on June 22, 2009 10:49 am

    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!

  17. djcomrad on June 22, 2009 11:04 am

    Already fixed the errors above..i added
    $xml = str_replace(”&”, “and”, $xml);
    $xml = str_replace(”#x26;”, “”, $xml);

    but it says undefined.

  18. rickadams on June 22, 2009 1:11 pm

    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.

  19. rickadams on June 22, 2009 10:59 pm

    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.

  20. djcomrad on June 23, 2009 11:23 am

    thanks rick! i just solved my problem. i have another question on how to put scroll in last songs played ?

  21. rickadams on July 1, 2009 10:12 pm

    Send me an email so I can ask you where you’ve got the station located.

  22. busby on August 22, 2009 9:53 am

    Hi Rick - is it possible to hack this script to give just the listener count, from several servers

  23. rickadams on August 24, 2009 10:29 pm

    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.

  24. tuomo on February 24, 2010 6:41 pm

    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

  25. rickadams on February 25, 2010 10:11 pm

    There’s a Javascript script in the index.html file that calls the playing.php script to plug in all the values.

  26. mikeyb on June 3, 2010 7:43 pm

    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.

  27. rickadams on June 3, 2010 10:56 pm

    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.

  28. ryansstuff on June 25, 2010 9:56 pm

    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?

  • ADS

  • ADS