我创建了一个获取休息xml响应的php代码。代码如下:
<table valign="top" cellpadding="10" style=" padding:0px 15px;"><tr></tr><tr>
<?php $song = str_replace(' ', '+', $post['title']);
$url = "http://ws.audioscrobbler.com/2.0/?method=chart.gettoptracks&limit=5&api_key=*************";
$xml=simplexml_load_file("$url");
foreach($xml->tracks->track as $track){
foreach($track->name as $name) {
$image = $track->image;
$link = str_replace(' ', '-', $name);
echo "<th width='20%' valign='top'><img src='$image' alt='$name' height='90' width='90'></br></br><a href='http://www.mp3jive.com/music/download/1/$link.html'>$name</a></th>";
}
}
?>
</tr>
</table>
它检索xml,如下所示:
<lfm status="ok">
<tracks page="1" perPage="5" totalPages="200" total="1000">
<track><name>Chandelier</name><duration>216</duration><playcount>109163</playcount><listeners>38691</listeners><mbid>4d507ca4-0d95-494c-b97b-e2fa50c926c1</mbid><url>http://www.last.fm/music/Sia/_/Chandelier</url><streamable fulltrack="0">0</streamable><artist><name>Sia</name><mbid>2f548675-008d-4332-876c-108b0c7ab9c5</mbid><url>http://www.last.fm/music/Sia</url></artist><image size="small">http://userserve-ak.last.fm/serve/34s/98738409.png</image><image size="medium">http://userserve-ak.last.fm/serve/64s/98738409.png</image><image size="large">http://userserve-ak.last.fm/serve/126/98738409.png</image><image size="extralarge">http://userserve-ak.last.fm/serve/300x300/98738409.png</image></track>
<track><name>Do I Wanna Know?</name><duration>273</duration><playcount>64234</playcount><listeners>33378</listeners><mbid>575d24ee-cf80-48ad-beeb-9295c7d05b35</mbid><url>http://www.last.fm/music/Arctic+Monkeys/_/Do+I+Wanna+Know%3F</url><streamable fulltrack="0">0</streamable><artist><name>Arctic Monkeys</name><mbid>ada7a83c-e3e1-40f1-93f9-3e73dbc9298a</mbid><url>http://www.last.fm/music/Arctic+Monkeys</url></artist><image size="small">http://userserve-ak.last.fm/serve/34s/92744747.png</image><image size="medium">http://userserve-ak.last.fm/serve/64s/92744747.png</image><image size="large">http://userserve-ak.last.fm/serve/126/92744747.png</image><image size="extralarge">http://userserve-ak.last.fm/serve/300x300/92744747.png</image></track>
<track><name>A Sky Full of Stars</name><duration>268</duration><playcount>60604</playcount><listeners>30131</listeners><mbid>d0d9e6e7-47c1-4470-9124-d10613c09ece</mbid><url>http://www.last.fm/music/Coldplay/_/A+Sky+Full+of+Stars</url><streamable fulltrack="0">0</streamable><artist><name>Coldplay</name><mbid>cc197bad-dc9c-440d-a5b5-d52ba2e14234</mbid><url>http://www.last.fm/music/Coldplay</url></artist><image size="small">http://userserve-ak.last.fm/serve/34s/99319661.png</image><image size="medium">http://userserve-ak.last.fm/serve/64s/99319661.png</image><image size="large">http://userserve-ak.last.fm/serve/126/99319661.png</image><image size="extralarge">http://userserve-ak.last.fm/serve/300x300/99319661.png</image></track>
<track><name>Stay with Me</name><duration>173</duration><playcount>70310</playcount><listeners>30071</listeners><mbid>c8dbf2c6-cbd2-48d2-ae04-e3098e29e306</mbid><url>http://www.last.fm/music/Sam+Smith/_/Stay+with+Me</url><streamable fulltrack="0">0</streamable><artist><name>Sam Smith</name><mbid>5a85c140-dcf9-4dd2-b2c8-aff0471549f3</mbid><url>http://www.last.fm/music/Sam+Smith</url></artist><image size="small">http://userserve-ak.last.fm/serve/34s/99230175.png</image><image size="medium">http://userserve-ak.last.fm/serve/64s/99230175.png</image><image size="large">http://userserve-ak.last.fm/serve/126/99230175.png</image><image size="extralarge">http://userserve-ak.last.fm/serve/300x300/99230175.png</image></track>
<track><name>Summer</name><duration>224</duration><playcount>57584</playcount><listeners>29519</listeners><mbid>9947d625-1d29-45df-a988-6e907fd1fea1</mbid><url>http://www.last.fm/music/Calvin+Harris/_/Summer</url><streamable fulltrack="0">0</streamable><artist><name>Calvin Harris</name><mbid>8dd98bdc-80ec-4e93-8509-2f46bafc09a7</mbid><url>http://www.last.fm/music/Calvin+Harris</url></artist><image size="small">http://userserve-ak.last.fm/serve/34s/97561221.png</image><image size="medium">http://userserve-ak.last.fm/serve/64s/97561221.png</image><image size="large">http://userserve-ak.last.fm/serve/126/97561221.png</image><image size="extralarge">http://userserve-ak.last.fm/serve/300x300/97561221.png</image></track>
</tracks>
</lfm>
抓取图像时此代码获取小图像。我想要抓住大图像。我怎样才能轻易改变它呢。感谢。
答案 0 :(得分:1)
由于大文件总是第3个,你可以简单地把:
foreach($xml->tracks->track as $track){
foreach($track->name as $name) {
$image = $track->image[2]; // it is the third image
$link = str_replace(' ', '-', $name);
echo "<th width='20%' valign='top'><img src='$image' alt='$name' height='90' width='90'></br></br><a href='http://www.mp3jive.com/music/download/1/$link.html'>$name</a></th>";
}
}
答案 1 :(得分:0)
最快的解决方案,通过索引访问图像:
$image = $track->image[2]; // large
$image = $track->image[3]; // extralarge