0

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">
http://img2-ak.lst.fm/i/u/34s/c14fdad46a5c423f86a683501c163c99.png
</image>
<image size="medium">
http://img2-ak.lst.fm/i/u/64s/c14fdad46a5c423f86a683501c163c99.png
</image>
<image size="large">
http://img2-ak.lst.fm/i/u/174s/c14fdad46a5c423f86a683501c163c99.png
</image>
<image size="extralarge">
http://img2-ak.lst.fm/i/u/300x300/c14fdad46a5c423f86a683501c163c99.png
</image>
<image size="mega">
http://img2-ak.lst.fm/i/u/c14fdad46a5c423f86a683501c163c99.png
</image>
<image size="">
http://img2-ak.lst.fm/i/u/arQ/c14fdad46a5c423f86a683501c163c99.png
</image>

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

pee2pee
  • 3,619
  • 7
  • 52
  • 133
  • Possible duplicate of [SimpleXML: Selecting Elements Which Have A Certain Attribute Value](http://stackoverflow.com/questions/992450/simplexml-selecting-elements-which-have-a-certain-attribute-value) – Mikel Bitson Oct 29 '15 at 14:36
  • That seems to bring back an array not containing the image – pee2pee Oct 29 '15 at 14:41

2 Answers2

0

This is possibly a duplicate of : SimpleXML: Selecting Elements Which Have A Certain Attribute Value

Here is what I would think for your particular case:

<?php
$xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Metallica&api_key=b25b959554ed76058ac220b7b2e0a026");
$largeImage = $xml->xpath('/lfm/artist/image[@size="mega"]')[0];
echo '<img src="'.$largeImage.'" />';     
?>
Community
  • 1
  • 1
Mikel Bitson
  • 3,583
  • 1
  • 18
  • 23
  • PHP Notice: Array to string conversion in C:\inetpub\wwwroot\lastfm.php on line 4. Also if I do use print_r the array is empty. The XML is http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Metallica&api_key=b25b959554ed76058ac220b7b2e0a026 – pee2pee Oct 29 '15 at 14:42
  • Hey there pee2pee- the XPath was incorrect, didn't include the base. I've edited it, hopefully that works for you. Again, the other stack overflow thread is still relevant, just google xpaths until you understand it? – Mikel Bitson Oct 29 '15 at 14:46
  • It still only brings back an empty array – pee2pee Oct 29 '15 at 14:48
  • Alrighty- good luck man, I hope you can figure out the xpath. I provided the answer to your question, just not the code for your particular use case. (In any case, this is a duplicate) I don't mind if you don't bother choosing an answer for this, no worries yo. – Mikel Bitson Oct 29 '15 at 14:57
  • I've tried all combinations and none of it works - baffling – pee2pee Oct 29 '15 at 15:00
  • I'm happy you figured it out! That's awesome. It's a bummer that you're trying to be so specific with your questions and answers man... SO really isn't for specific code, it's for concepts. This should assist someone in the future with a similar issue, and surely their issue won't be exactly what you've got. – Mikel Bitson Oct 29 '15 at 17:45
0
<?php
$xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Metallica&api_key=b25b959554ed76058ac220b7b2e0a026");
$largeImage = $xml->xpath('/lfm/artist/image[@size="mega"]')[0];
echo '<img src="'.$largeImage.'" />'; 
?>
pee2pee
  • 3,619
  • 7
  • 52
  • 133