简体   繁体   English

PHP Last.fm为艺术家获得最大的图像

[英]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. 这将以第7个节点为目标-但是第7个节点可能不存在,因此将不起作用。 Is there anyway to specifically target the large, extralarge or mega nodes? 无论如何,是否有针对性的针对大型,超大型或大型节点?

Example XML 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 因此,如果不存在mega,则进行超大型;如果不存在,则进行大型化,等等。

This is possibly a duplicate of : SimpleXML: Selecting Elements Which Have A Certain Attribute Value 这可能是以下内容的重复: SimpleXML:选择具有特定属性值的元素

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM