XML feed on site

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
pimhd
Forum Members
Forum Members
Posts: 19
Joined: Mon Aug 28, 2006 12:47 pm

XML feed on site

Post by pimhd »

is it possible to display the contents of a last.fm XML feed on my CMSMS site?

I know of RSS2HTML but as far as I know can only handle rss files.

The XMl from lastfm looks bit like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?> 
- <topartists user="user" type="overall">
- <artist>
  <name>Ry Cooder</name> 
  <mbid>a4e2650b-938b-4b72-95bc-6e229cd34601</mbid> 
  <playcount>5</playcount> 
  <rank>1</rank> 
  <url>http://www.last.fm/music/Ry+Cooder</url> 
  <thumbnail>http://userserve-ak.last.fm/serve/34/118302.jpg</thumbnail> 
  <image>http://userserve-ak.last.fm/serve/126/118302.jpg</image> 
  </artist>

 
Last edited by pimhd on Fri Sep 12, 2008 1:09 pm, edited 1 time in total.
nhaack

Re: XML feed on site

Post by nhaack »

Hey pimhd,

probably this user defined tag I quickly made can give you a start:

Code: Select all


$lastfmfeed = simplexml_load_file($params[url]);
 
foreach ($lastfmfeed ->topartists->artist as $artist) 

{

$output .= $artist->name.'<br />'."\n";
$output .= $artist->playcount.'<br />'."\n";
$output .= $artist->url.'<br />'."\n";
$output .= $artist->name.'<br />'."\n";
$output .= '<img src="'.$artist->image[0].'" />'."\n";
$output .= '<img src="'.$artist->image[1].'" />'."\n";
$output .= '<img src="'.$artist->image[2].'" />'."\n";
$output .= '<hr />';

}

echo $output;

Save this as a user tag with the name lastfmfeed and call the tag from your content or template like in the example below:

Code: Select all

{lastfmfeed url="http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=rj&api_key=b25b959554ed76058ac220b7b2e0a026"}
However, to use this, you need to have php5 and allow_url_fopen = On in your php.ini to allow simplexml to access external urls.

The script gets the xml from the url, looks for the node "topartists" in the xml document, and then walks trough the list of nodes with the name "artist". Afterwards it prints the value of selected variables for each artist entry.

For the code above I used a sample API 2.0 feed from last.fm (I think yours above was 1.0). But anyway, I guess the code can give you some directions and you can quickly alter it to match your needs.

If you want to access a specific element (e.g. the name of the newest artist in the list), you do not need to cycle through the elements. This should give you a start:

Code: Select all

$lastfmfeed = simplexml_load_file($params[url]);
echo $lastfmfeed ->topartists->artist[0]->name;
Let me know how it turned out for you.

Best
Nils
pimhd
Forum Members
Forum Members
Posts: 19
Joined: Mon Aug 28, 2006 12:47 pm

Re: XML feed on site

Post by pimhd »

Thank you very much for your help. I've tried your solution but i could not get it to work for me.

When I call the tag using your code
{lastfmfeed url="http://ws.audioscrobbler.com/2.0/?metho ... b7b2e0a026"}

it is working, but when i try my own url, all i get is an empty page.
the code i'm using is

{lastfmfeed url="http://ws.audioscrobbler.com/1.0/user/O ... .xml?type="}

Is the URL i'm using incorrect?
nhaack

Re: XML feed on site

Post by nhaack »

Hey pimhd,

this would be the user tag to simply output all elements of the lastfm xml feed you mentioned.

Code: Select all


$lastfmfeed = simplexml_load_file($params[url]);
 
foreach ($lastfmfeed->artist as $artist) 

{

$output .= $artist->name.'<br />'."\n";
$output .= $artist->playcount.'<br />'."\n";
$output .= $artist->rank.'<br />'."\n";
$output .= $artist->url.'<br />'."\n";
$output .= $artist->thumbnail.'<br />'."\n";
$output .= $artist->image.'<br />'."\n";

$output .= '<hr />';

}

echo $output;

So the URL you where using is not incorrect ;) The code I made was just oriented at the newer APIs response. The code above should get you going with the feed you have.

This is the structure of your feed:

Code: Select all

  <?xml version="1.0" encoding="UTF-8" ?> 
<topartists user="Okki" type="overall">
- <artist>
  <name>The Beatles</name> 
  <mbid>b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d</mbid> 
  <playcount>473</playcount> 
  <rank>1</rank> 
  <url>http://www.last.fm/music/The+Beatles</url> 
  <thumbnail>http://userserve-ak.last.fm/serve/34/407719.jpg</thumbnail> 
  <image>http://userserve-ak.last.fm/serve/126/407719.jpg</image> 
  </artist>
- <artist>...
- <artist>...
- <artist>...
</topartists>
I think you can easily spot the names of the items in the code above and find them in the user tag. You url didn't work in the first place, because the tag was expecting a slightly different hierarchy. Look at the source of the URL of my first example. Compare both URLs sources and compare the two tags and should get a first idea of how you can handle nearly every XML input.

If you want to drive it a little further to understand what you can do with XML data (for a little inspiration) I recommend making yourself a little familiar with simpleXML http://de3.php.net/simplexml for the beginning :)

Enjoy

Best
Nils
Last edited by nhaack on Sat Sep 13, 2008 12:19 am, edited 1 time in total.
essexboyracer
Forum Members
Forum Members
Posts: 85
Joined: Wed Jun 20, 2007 5:40 pm

Re: XML feed on site

Post by essexboyracer »

I have used magpierss before to grab RSS feeds, dont know if it could be used here for an XML feed (aren´t they the same thing?)

http://magpierss.sourceforge.net/

If you think it will work, i can post the example, all wrapped up in s UDT
viebig

Re: XML feed on site

Post by viebig »

look for lassrss on this forum, I´ve wrote a solution
nhaack

Re: XML feed on site

Post by nhaack »

mhhh... lastRSS sounds like an interesting option. I especially like that it has caching!

But I think it can only handle RSS style XML, or am I wrong? I'm asking because the feed pimhd mentioned is not rss. Is it possible to adapt lastRSS easily to other (multiple) XML structures?

cheers
nils
viebig

Re: XML feed on site

Post by viebig »

my bad.. i think http://www.bin-co.com/php/scripts/xml2array/ is easier than adapt lastrss.

If you to go deep, you can implement this function inside lastrss and have cache too
pimhd
Forum Members
Forum Members
Posts: 19
Joined: Mon Aug 28, 2006 12:47 pm

Re: XML feed on site

Post by pimhd »

Thank you all, especially nhaack. I really appreciate your helpfullness.

the method of nhaack works perfectly for me. If i have time i will look at xml2array to see what that does.
nhaack

Re: XML feed on site

Post by nhaack »

Nice to hear that it worked out for you.

@viebig: the caching mechanism inspired me... I have created a new UDT, see here http://forum.cmsmadesimple.org/index.php/topic,25781.0.html
Post Reply

Return to “Modules/Add-Ons”