This has been driving me insane for about the last hour. I’m trying to parse a bit of XML out of Last.fm’s API, I’ve used about 35 different permutations of the code below, all of which have failed. I’m really bad at XML parsing, lol. Can anyone help me parse the first toptags>tag>name ‘name’ from this XML API in PHP? ๐
Which in that case ^ would be ‘electronic’
Right now, all I have is this
<?
$xmlstr = file_get_contents("http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=Owl+city&track=fireflies");
$genre = new SimpleXMLElement($xmlstr);
echo $genre->lfm->track->toptags->tag->name;
?>
Which returns with, blank. No errors either, which is what’s incredibly annoying!
Thank You very Much ๐ ๐ ๐
Any help greatly, and by greatly I mean really, really greatly appreciated! ๐
The
<tag>tag is an array, so you should loop through them with aforeachor similar construct. In your case, just grabbing the first would look like this:Also note that the
<lfm>tag is not needed.UPDATE
I find it’s much easier to grab exactly what I’m looking for in a SimpleXMLElement by using
print_r(). It’ll show you what’s an array, what’s a simple string, what’s another SimpleXMLElement, etc.