Get large artist image from last.fm xml (api artist.getinfo)

 
    • [Raderad användare] sa...
    • Användare
    • 7 apr 2012, 10:22

    Get large artist image from last.fm xml (api artist.getinfo)

    This is an example xml response from last fm (artist Adele):

    <lfm status="ok">
    <artist>
    <name>Adele</name>
    <mbid>1de93a63-3a9f-443a-ba8a-a43b5fe0121e</mbid>
    <url>http://www.last.fm/music/Adele</url>;
    <image size="small">http://userserve-ak.last.fm/serve/34/71796928.png</image>;
    <image size="medium">http://userserve-ak.last.fm/serve/64/71796928.png</image>;
    <image size="large">http://userserve-ak.last.fm/serve/126/71796928.png</image>;
    <image size="extralarge">http://userserve-ak.last.fm/serve/252/71796928.png</image>;
    <image size="mega">http://userserve-ak.last.fm/serve/_/71796928/Adele+PNG.png</image>;
    ...


    I'm trying to echo that large image, but it doesn't return anything when I do this:

    <?php
    $xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=adele&api_key=b25b959554ed76058ac220b7b2e0a026";);

    $largeimg = $xml->artist->image['large'];
    echo '<img src="'.$largeimg.'" />';
    ?>


    If I just put $largeimg = $xml->artist->image; it grabs that first image (small one). Any idea how I can fix this? Thank you!

    • tburny sa...
    • Forum Moderator
    • 7 apr 2012, 11:37
    I would use xpath for this, see http://php.net/manual/de/simplexmlelement.xpath.php
    $result = $xml->xpath("//image[@size="mega]/text()")
    This would select the content of all image nodes with a size="mega" attribute.
    You only would have to fetch the first result, done :)

    Combo.fm: Combine your favourite radio stations! | My Blog | scala-lastfmapi | Cache2k - A high performance Java in-memory cache
    P.S.: Do not click here
    throw new PokemonException(); //Gotta catch 'em all
    My forum post reflects my personal opinion :)
  • ditto

    im getting the same problem, and that xpath sample you posted brings up an error :s quite confused by this really

    • moominfm sa...
    • Användare
    • 12 maj 2012, 10:02
    Hi guys,
    does somebody know is it possible to add artists from mentioned image xml list (api library.getArtists) to another account using api library.addArtists without building any app or something? Maybe there is some third-party app already written?

    • Zorgsky sa...
    • Användare
    • 25 maj 2012, 09:30

    Re: ditto

    danielsailes escribió:
    im getting the same problem, and that xpath sample you posted brings up an error :s quite confused by this really


    $recartists=simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=library.getartists&api_key=b25b959554ed76058ac220b7b2e0a026&user=Zorgsky');

    $artsimg2 = $recartists->xpath("//image[@size='extralarge']");

    Cualquier cosa de este día.
  • Re: Re: ditto

    Zorgsky said:
    danielsailes escribió:
    im getting the same problem, and that xpath sample you posted brings up an error :s quite confused by this really


    $recartists=simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=library.getartists&api_key=b25b959554ed76058ac220b7b2e0a026&user=Zorgsky');

    $artsimg2 = $recartists->xpath("//image[@size='extralarge']");


    Me arroja: "Array"

  • Re: Get large artist image from last.fm xml (api artist.getinfo)

    (It's maybe a bit late for the answer, but it could help someone, someday) The first code is quite correct, it's just that simplexml does not care about the "real" value of the attribute <image size="">, if it sees several lines with the same attribute but different propreties setted, it will store it into an array, so you can access to it with ->image[0-4]

    So for the large image, the code will be <?php
    $xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=adele&api_key=b25b959554ed76058ac220b7b2e0a026";);

    $largeimg = $xml->artist->image[2];
    echo '<img src="'.$largeimg.'" />';
    ?>

Anonyma användare kan inte skriva inlägg. Vänligen logga in eller skapa ett konto för att göra inlägg i forumen.