繁体   English   中英

PHP和Last.fm API

[英]PHP and Last.fm API

我有一个JSON结构,如http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=XXXX&format=json ,我想挑选出类似艺术家的各种信息(姓名和图像),标签,超大图像,内容等

我得到了类似的arists工作使用

<?php

$url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Queens%20Of%20the%20STone%20Age&api_key=XXX&format=json';
$content = file_get_contents($url);
$json = json_decode($content, true);

foreach($json['artist']['similar']['artist'] as $item) {
    print $item['name'];
    print '<br>';
}
?>

但是,如何从以下内容中提取“大”图像:

"artist": [{
     "name": "Them Crooked Vultures",
      "url": "http:\/\/www.last.fm\/music\/Them+Crooked+Vultures",
       "image": [{
            "#text": "http:\/\/userserve-ak.last.fm\/serve\/34\/38985285.jpg",
            "size": "small"
            }, {
            "#text": "http:\/\/userserve-ak.last.fm\/serve\/64\/38985285.jpg",
            "size": "medium"
            }, {
            "#text": "http:\/\/userserve-ak.last.fm\/serve\/126\/38985285.jpg",
            "size": "large"
       }]

谢谢,

JJ

全部排序! 成品: http//www.strictlyrandl.com/artist/queens-of-the-stone-age/

您需要遍历图像并打印它:

foreach($json['artist']['similar']['artist'] as $item) {
    print $item['name'];
    print '<br>';

    for ($i=0; $i < count($item['image']); $i++) { 
        echo $item['image'][$i]['#text']."<br>";
    }
}

要仅在尺寸较大或超大时打印它们,您可以使用简单的if语句:

for ($i=0; $i < count($item['image']); $i++) { 
    echo $item['image'][$i]['#text']."<br>";
    if($item['image'][$i]['size'] == 'extralarge') {
        echo $item['image'][$i]['#text']."<br>";
    }
}
$get = file_get_contents('http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=63692beaaf8ba794a541bca291234cd3&format=json');
$get = json_decode($get);
echo '<pre>';
//print_r($get->artist);
echo '</pre>';

echo '<strong>Artist Name = </strong>'.$get->artist->name.'<br/>
<strong>Artist mbid = </strong>'.$get->artist->mbid.'<br/>
<strong>Artist Url = </strong>'.$get->artist->url.'<br/>
<strong>Artist İmage = </strong><br/>';
foreach($get->artist->image as $image) {
$image = (array) $image;
    echo 'İmage Text = '.$image['#text'].'<br />
        İmage Size = '.$image['size'].'<br />';
}

暂无
暂无

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

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