rsxdev Posted May 5, 2012 Share Posted May 5, 2012 I need PHP script that will parse XML-based (.xspf format) live stream playlist stored on web server, retrieve track data and print it on my web page. Print inside div as scrolling text (one line). The track data on page need be refreshed synchronously to show current track info. XSPF playlist example: <?xml version="1.0" encoding="UTF-8"?> <playlist xmlns="http://xspf.org/ns/0/" version="1"> <title/> <creator/> <trackList> <track> <location>http://geodesid5.streams.audioveduct.com:80/di_ambient7</location> <title>Bruno Sanfilippo - Piano Textures 2 II</title> <annotation>Stream Title: Ambient - DIGITALLY IMPORTED - a blend of ambient, downtempo, and chillout Content Type:audio/mpeg Current Listeners: 0 Peak Listeners: 0 Stream Genre: Electronic Ambient Downtempo</annotation> <info>http://www.di.fm</info> </track> </trackList> </playlist> I need get data from fields 'title' and 'annotation' (only text between Stream Title and Content Type). Also, after these two, I want scroll small custom text, when required. I searched some close example, but didn't find yet. Will be DOMDocument method suitable for this task? Can someone show me the right method to do this? Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/ Share on other sites More sharing options...
xyph Posted May 5, 2012 Share Posted May 5, 2012 I'd suggest SimpleXML <?php $xmldata = '<?xml version="1.0" encoding="UTF-8"?> <playlist xmlns="http://xspf.org/ns/0/" version="1"> <title/> <creator/> <trackList> <track> <location>http://geodesid5.streams.audioveduct.com:80/di_ambient7</location> <title>Bruno Sanfilippo - Piano Textures 2 II</title> <annotation>Stream Title: Ambient - DIGITALLY IMPORTED - a blend of ambient, downtempo, and chillout Content Type:audio/mpeg Current Listeners: 0 Peak Listeners: 0 Stream Genre: Electronic Ambient Downtempo</annotation> <info>http://www.di.fm</info> </track> </trackList> </playlist>'; $xml = new SimpleXMLElement($xmldata); foreach( $xml->trackList->track as $track ) { echo $track->title .' - '. $track->annotation .'<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1343199 Share on other sites More sharing options...
rsxdev Posted May 5, 2012 Author Share Posted May 5, 2012 well, but I need process URL, not code. So I need get xml document into SimpleXML: <?php $playlistUrl = 'http://path/to/playlist.xspf'; $xmldata = file_get_contents($playlistUrl); $xml = new SimpleXMLElement($xmldata); foreach( $xml->trackList->track as $track ) { echo $track->title .' - '. $track->annotation .'<br>'; } ?> I need not all data from 'annotation' field, but only part I specified. How to filter it out correctly? Also how to implement custom text and scrolling function? Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1343204 Share on other sites More sharing options...
xyph Posted May 5, 2012 Share Posted May 5, 2012 http://www.php.net/manual/en/function.simplexml-load-file.php I need not all data from 'annotation' field, but only part I specified. How to filter it out correctly? Also how to implement custom text and scrolling function? explode by newline, grab first and third result Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1343209 Share on other sites More sharing options...
rsxdev Posted May 5, 2012 Author Share Posted May 5, 2012 XMLReader class seems will be better for this task. Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1343369 Share on other sites More sharing options...
rsxdev Posted May 10, 2012 Author Share Posted May 10, 2012 need be endforeach added to the end? <?php $playlistUrl = 'http://path/to/playlist.xspf'; $xmldata = file_get_contents($playlistUrl); $xml = new SimpleXMLElement($xmldata); foreach( $xml->trackList->track as $track ) { echo $track->title .' - '. $track->annotation .'<br>'; endforeach; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1344545 Share on other sites More sharing options...
xyph Posted May 10, 2012 Share Posted May 10, 2012 The syntax is available directly in the manual. http://php.net/manual/en/control-structures.foreach.php The alternate syntax is mentioned in the User Comments More can be read about it here http://php.net/manual/en/control-structures.alternative-syntax.php At the moment, you're mixing standard and alternate, which doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1344550 Share on other sites More sharing options...
rsxdev Posted May 10, 2012 Author Share Posted May 10, 2012 explode by newline, grab first and third result can you show an explode example? The Example1 from http://php.net/explode is too generic. For simplicity, I want grab this part "Stream Genre: Electronic Ambient Downtempo" Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1344583 Share on other sites More sharing options...
xyph Posted May 10, 2012 Share Posted May 10, 2012 <?php $string = 'This is some text on multiple lines oh how could I split this data into several parts'; # Each OS likes to use it's own linebreaks # UNIX uses \n # Mac uses \r (not sure about OSX, as it's unix-based) # Windows uses \r\n # You can use the constant, PHP_EOL, but it's not guarenteed. I usually code # in Windows, but my text-editor uses \n only for linebreaks # To get them all into the same format, I use the following snippet. $string = str_replace(array("\r\n","\r"),"\n",$string); # This snippet first replaces any instance of \r\n to \n, then looks for any # rogue \r's to convert to \n $array = explode("\n", $string); print_r($array); ?> Output Array ( [0] => This is some text [1] => on multiple lines [2] => oh how could I split this data [3] => into several parts ) Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1344590 Share on other sites More sharing options...
rsxdev Posted May 10, 2012 Author Share Posted May 10, 2012 will it count as '\n' new line every tab between words inside <annotation> </annotation> tags?' Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1344626 Share on other sites More sharing options...
xyph Posted May 10, 2012 Share Posted May 10, 2012 What? "\t" is for tabs. And you MUST double quote "\n" to convert it to a linebreak. '\n' is the literal string. Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1344629 Share on other sites More sharing options...
rsxdev Posted May 11, 2012 Author Share Posted May 11, 2012 oh yes "\n" character as a newline delimiter. For simplicity, I want grab full first line after <annotation> tag (before Content Type:). not sure will this be right: $metadata->trackList->annotation as $annotation $parts = explode("\n", $metadata); echo $parts[0]; I need echo it after track title, in one line. Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1344811 Share on other sites More sharing options...
xyph Posted May 11, 2012 Share Posted May 11, 2012 Try it out It looks like it would work. Quote Link to comment https://forums.phpfreaks.com/topic/262100-php-script-to-parse-xml-based-playlist/#findComment-1344815 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.