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

 
    • [Usuário excluído] disse...
    • Usuário
    • Abr 7 2012, 10h22

    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 disse...
    • Forum Moderator
    • Abr 7 2012, 11h37
    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 disse...
    • Usuário
    • Mai 12 2012, 10h02
    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 disse...
    • Usuário
    • Mai 25 2012, 9h30

    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.'" />';
    ?>

    • waxs disse...
    • Usuário
    • Jan 27 2015, 21h32

    ...fixed

    angenoir71880 you helped me out, anyway. Thanks!

Usuários anônimos não podem postar mensagens. É preciso fazer login ou criar uma conta para postar nos fóruns.