php - How can I re-parse an XML document at set intervals? -
i have built area on client's website displays song playing, parsed xml file via php. however, client wants song auto-refresh instead of him having manually refresh page.
i played around parsing file using jquery.get() , .ajax(), because of way xml file structured, seems though can artist , name squashed 1 string, or when try specific returns [object object].
i haven't tried tackle having song's length calculated , refresh feed based on length. may not seeing apparently such issue me.
any or general guidance appreciated.
example working php code (obviously, non-ajax):
<?php $recentlyplayed = simplexml_load_file('http://publicapi.streamtheworld.com/public/nowplaying?mountname=abcdfm&numbertofetch=1&eventtype=track'); $tracktitle = $recentlyplayed->{'nowplaying-info'}[0]->property[1]; $trackartist = $recentlyplayed->{'nowplaying-info'}[0]->property[0]; echo "<h6>" . $trackartist . "<span class=\"song-title\">" . $tracktitle . "</span></h6>"; ?>
i've tried several different things work, seems initial obstacle trying reference data in xml file using attributes, rather node-names. nodes named same, , it's attributes differentiate them. so, such code render correctly, unless artist/song title blank, renders third field sort of cryptically-named "cue_time_start".
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script> <script type="text/javascript"> $(document).ready(function() { setinterval("songpull()",1000); }); function songpull() { $.get( "http://publicapi.streamtheworld.com/public/nowplaying?mountname=abcdfm&numbertofetch=1&eventtype=track", "xml", function(data) { $(data).find("nowplaying-info").each(function(){ var artist = $(this).find("property").eq(0).text(); var title = $(this).find("property").eq(1).text(); $('body').html("<h1>" + artist + "<small class=\"song-title\">" + title + "</small></h1>"); console.log (artist); console.log (title); }); } ); } </script> <body> </body>
any guidance, advice or examples of best practices when trying sort of thing appreciated.
i'm not sure if want, use attribute selectors extract data want out of xml document.
$.get("http://publicapi.streamtheworld.com/public/nowplaying?mountname=kroxfm&numbertofetch=1&eventtype=track", "xml", function(data) { var $nowplaying = $(data).find('nowplaying-info'); console.log($nowplaying.find('[name=track_artist_name]').text()); console.log($nowplaying.find('[name=cue_title]').text()); } );
also, never pass string
setinterval
or settimeout
, can pass function reference directly:
setinterval(songpull ,1000);
Comments
Post a Comment