Jump to content

PHP script to parse xml-based playlist


rsxdev

Recommended Posts

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?

 

 

 

Link to comment
Share on other sites

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>';
}

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

<?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
)

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.