radio1017x Posted December 31, 2015 Share Posted December 31, 2015 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> <AlbumArt>http://radio1017x.com/imgs/nothingbuthieves-nothingbutthieves.jpg</AlbumArt> <Purchase>https://geo.itunes.apple.com/us/album/trip-switch/id1016210775?i=1016210784&at=1l3vnI8&mt=1&app=music</Purchase></Current></NowPlaying> 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? Quote Link to comment https://forums.phpfreaks.com/topic/300079-my-radio-station-website-needs-your-help/ Share on other sites More sharing options...
Barand Posted December 31, 2015 Share Posted December 31, 2015 (edited) 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 December 31, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/300079-my-radio-station-website-needs-your-help/#findComment-1528910 Share on other sites More sharing options...
radio1017x Posted December 31, 2015 Author Share Posted December 31, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/300079-my-radio-station-website-needs-your-help/#findComment-1528914 Share on other sites More sharing options...
Barand Posted December 31, 2015 Share Posted December 31, 2015 If you don't want to refresh the page then you will need AJAX Quote Link to comment https://forums.phpfreaks.com/topic/300079-my-radio-station-website-needs-your-help/#findComment-1528915 Share on other sites More sharing options...
radio1017x Posted December 31, 2015 Author Share Posted December 31, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/300079-my-radio-station-website-needs-your-help/#findComment-1528916 Share on other sites More sharing options...
Barand Posted January 1, 2016 Share Posted January 1, 2016 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> Quote Link to comment https://forums.phpfreaks.com/topic/300079-my-radio-station-website-needs-your-help/#findComment-1528928 Share on other sites More sharing options...
radio1017x Posted January 4, 2016 Author Share Posted January 4, 2016 Dude, thank you so much. I'm going to play around with this for a while and see if I can't just make it work. Quote Link to comment https://forums.phpfreaks.com/topic/300079-my-radio-station-website-needs-your-help/#findComment-1529085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.