PHP Last.fm Get Largest Image for an Artist

I have the following code

<?php
    $xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Metallica&api_key=b25b959554ed76058ac220b7b2e0a026");
    $artistTag= $xml->artist->children();
    $largeImage = $artistTag[7];
    echo '<img src="'.$largeImage.'" />';     
?>

This will target the 7th node – however the 7th node might not exist so this won’t work. Is there anyway to specifically target the large, extralarge or mega nodes?

Example XML

<lfm status="ok">
<artist>
<name>Metallica</name>
<mbid>65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab</mbid>
<url>http://www.last.fm/music/Metallica</url>
<image size="small">

</image>
<image size="medium">

</image>
<image size="large">

</image>
<image size="extralarge">

</image>
<image size="mega">

</image>
<image size="">

</image>

So if mega doesn’t exist, go for extralarge, if that doesn’t exist, go to large etc

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s