PHP和Last.fm API [英] PHP and Last.fm API

查看:98
本文介绍了PHP和Last.fm API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有JSON结构,例如

I have a JSON structure such as http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=XXXX&format=json and I'd like to pick out various bits of information such as similar artists (names and images), tags, the extralarge image, content etc

我通过使用得到了类似的阿里斯特斯

I got the similar arists working by using

<?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>';
}
?>

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

However, how do I extract the "large" image from the following:

"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石头时代/

推荐答案

您需要遍历图像并进行打印:

You'll need to loop through the images and print it:

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语句:

To print them only if they're of size large or extra large, you can use a simple if statement:

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>";
    }
}

这篇关于PHP和Last.fm API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆