Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 格式化xml rest返回_Php_Html_Xml_Rest - Fatal编程技术网

Php 格式化xml rest返回

Php 格式化xml rest返回,php,html,xml,rest,Php,Html,Xml,Rest,我创建了获取RESTXML响应的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.gettopt

我创建了获取RESTXML响应的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>


因为大文件总是第三个,所以您可以简单地放:

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>";
    }
}
foreach($xml->tracks->track as$track){
foreach($track->name as$name){
$image=$track->image[2];//这是第三个映像
$link=str_replace(“”,“-”,$name);
回声“

”; } }
最快的解决方案是通过索引访问图像:

$image = $track->image[2]; // large

$image = $track->image[3]; // extralarge

您真的不应该将API密钥放在任何地方。
foreach($track->image as$image){$attrs=$image->attributes();if($attrs['size']='large')(…
)或使用XPath表达式进行获取。
$image = $track->image[2]; // large

$image = $track->image[3]; // extralarge