Jump to content

Recommended Posts

I am not a real deal programmer, I can make simple webpages that work, do some basic coding, but nothing more than html or simple php.  If one of you fine folks could please help me, I am in need of a simple piece of code for my listen live page for my radio station's website.  I have an on-air automation system that generates an xml file as each song plays, looks like this:

 

 

<?xml version="1.0"?>
<NowPlaying>
<Current>
<Station>KVXX</Station>
<Artist>NOTHING BUT THIEVES</Artist>
<Title>TRIP SWITCH</Title>
<Album>NOTHING BUT THIEVES</Album>
<Duration>03:00</Duration>
<Category>Music</Category>
<RecordLabel>RCA</RecordLabel>
<Composer/>
<CutNumber>11940</CutNumber>
<StartTime>12/31/2015 11:29:22 AM</StartTime>

 

So, basically all I need the piece of code to do is read certain lines from the XML when it updates and spit the text out on to the page automatically.  Artist, Title, Album, Duration, and RecordLabel just need to be printed to the page as text, AlbumArt is a link to the current song's album art, which needs to load as well, and Purchase is a link to itunes for the listener to click on and go directly to buy the song, so that field is to be used as a link url.  

 

Is this simple to do?  Do I need ajax for this or will normal php do?  

Take a look at simplexml library

 

http://php.net/manual/en/book.simplexml.php

 

Example

$str = '<?xml version="1.0"?>
<NowPlaying>
<Current>
<Station>KVXX</Station>
<Artist>NOTHING BUT THIEVES</Artist>
<Title>TRIP SWITCH</Title>
<Album>NOTHING BUT THIEVES</Album>
<Duration>03:00</Duration>
<Category>Music</Category>
<RecordLabel>RCA</RecordLabel>
<Composer/>
<CutNumber>11940</CutNumber>
<StartTime>12/31/2015 11:29:22 AM</StartTime>
<AlbumArt>http://radio1017x.co....jpg</AlbumArt>
<Purchase>https://geo.itunes.a...t</Purchase>
</Current>
</NowPlaying>';

$xml = simplexml_load_string($str);

echo "Title: {$xml->Current->Title}<br>";
echo "Artist: {$xml->Current->Artist}<br>";
echo "Duration: {$xml->Current->Duration}<br>";
Edited by Barand

I actually had it set up pretty much exactly like that, but I'm using simplexml_load_file("nowplaying.xml") and it wasn't working, it turns out there was an error on my server that they are looking into right now to make that function work

 

this is my php:

 

 <?php
$xml=simplexml_load_file("nowplaying.xml") or die("Error: Cannot create object");
echo "Artist: {$xml->Artist}<br>";
echo "Title: {$xml->Title}<br>";
echo "Album: {$xml->Album}<br>";
echo "Length: {$xml->Duration}<br>";
echo "Label: {$xml->RecordLabel}<br>";
echo "Buy this song: <a href={$xml->Purchase}>{$xml->Artist} - {$xml->Title}</a>";
?>
 
 
 
But will this automatically refresh the information on the page when the xml file updates every few minutes?  I don't want the listener to have to refresh the page to get the next song title, because that will stop the audio player.

That was my original question, I thought so.  And I know nothing about Ajax.  Awesome.  Is there a decent tutorial or something I can look at to start trying to figure that out?  Or is it simple enough that someone might help me out with a sample of code?

Easiest way to use AJAX is with jquery. Below is an example. Whatever output the php script echos will be sent back to the calling function in the "data" variable where it is then displayed in the div.

<html>
<head>
<title>Sample</title>
<script type='text/javascript' src='jquery-1.10.2.js'></script>
<script type='text/javascript'>
    function getNowPlaying()
    {   
        // send ajax request to nowplaying.php
        // and put output in div #playing
        $.get (
            "nowplaying.php",
            "",
            function(data) {
                $("#playing").html(data);
            },
            "text"
            )
    }
    
    $().ready(function() {
        getNowPlaying();  // call when page has loaded
        setInterval("getNowPlaying()", 10000);  // then call function every 10sec
    })
</script>
</head>
<div id="playing"></div>
</html>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.