这是我第一次在这里提问,我也是PHP和简单XML的新手,但我决心要学习这个。我已经搜索了之前提出过的所有相关问题,但是我很难将它应用到我的情况,尽管看起来很相似。真是令人沮丧:(。
我正在尝试以XML格式从Amazon Cloudsearch读取API响应。
以下是原始的XML响应:
<results xmlns="http://cloudsearch.amazonaws.com/2011-02-01/results">
<rank>-text_relevance</rank>
<match-expr>(label '"mango"')</match-expr>
<hits found="99" start="0">
<hit id="mango1to100_csv_31">
<d name="imageurl">
http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/66650066_51.jpg
</d>
</hit>
<hit id="mango1to100_csv_10">
<d name="imageurl">
http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650060_34.jpg
</d>
</hit>
<hit id="mango1to100_csv_11">
<d name="imageurl">
http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650062_MQ.jpg
</d>
</hit>
<hit id="mango1to100_csv_12">
<d name="imageurl">
http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650067_58.jpg
</d>
</hit>
<hit id="mango1to100_csv_13">
<d name="imageurl">
http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650034_02.jpg
</d>
</hit>
<hit id="mango1to100_csv_14">
<d name="imageurl">
http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650036_MQ.jpg
</d>
</hit>
<hit id="mango1to100_csv_15">
<d name="imageurl">
http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650038_34.jpg
</d>
</hit>
<hit id="mango1to100_csv_16">
<d name="imageurl">
http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650039_39.jpg
</d>
</hit>
<hit id="mango1to100_csv_17">
<d name="imageurl">
http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650070_33.jpg
</d>
</hit>
<hit id="mango1to100_csv_18">
<d name="imageurl">
http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650040_02.jpg
</d>
</hit>
</hits>
<facets/>
<info rid="12345" time-ms="3" cpu-time-ms="0"/>
</results>
我使用SimpleXML开始:
SimpleXMLElement Object
(
[rank] => -text_relevance
[match-expr] => (label '"mango"')
[hits] => SimpleXMLElement Object
(
[@attributes] => Array
(
[found] => 99
[start] => 0
)
[hit] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mango1to100_csv_31
)
[d] => http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/66650066_51.jpg
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mango1to100_csv_10
)
[d] => http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650060_34.jpg
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mango1to100_csv_11
)
[d] => http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650062_MQ.jpg
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mango1to100_csv_12
)
[d] => http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650067_58.jpg
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mango1to100_csv_13
)
[d] => http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650034_02.jpg
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mango1to100_csv_14
)
[d] => http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650036_MQ.jpg
)
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mango1to100_csv_15
)
[d] => http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650038_34.jpg
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mango1to100_csv_16
)
[d] => http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650039_39.jpg
)
[8] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mango1to100_csv_17
)
[d] => http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650070_33.jpg
)
[9] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => mango1to100_csv_18
)
[d] => http://st.mngbcn.com/rcs/pics/static/T6/fotos/S1/63650040_02.jpg
)
)
)
[facets] => SimpleXMLElement Object
(
)
[info] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rid] => 12345
[time-ms] => 3
[cpu-time-ms] => 0
)
)
)
我正在尝试根据结果中提供的URL读取并显示所显示的每个项目的图像。我知道我应该在这里使用“foreach”循环,但我似乎无法正确使用它,所以这是我做的很长的路:
<?php
$feed_url = 'http://www.myapicallexample.com';
$feed = simplexml_load_file($feed_url);
echo "<ul>";
echo "<li><img src='", $feed->hits[0]->hit[1]->d,"'></li>";
echo "<li><img src='", $feed->hits[0]->hit[2]->d,"'></li>";
echo "<li><img src='", $feed->hits[0]->hit[3]->d,"'></li>";
echo "<li><img src='", $feed->hits[0]->hit[4]->d,"'></li>";
echo "<li><img src='", $feed->hits[0]->hit[5]->d,"'></li>";
echo "<li><img src='", $feed->hits[0]->hit[6]->d,"'></li>";
echo "<li><img src='", $feed->hits[0]->hit[7]->d,"'></li>";
echo "</ul>";
?>
我尝试了foreach语句的各种组合,从查看其他问题的答案,但没有任何作用。我准备把电脑扔出窗外了。提前致谢!
附录:以下是我之前多次尝试过的愚蠢行为之一的示例:)
<?php
$feed_url = 'http://www.myapicallexample.com';
$xml = simplexml_load_file($feed_url);
$imageurl = $xml->hits[0]->hit[1]->d;
foreach($imageurl as $a)
{
echo $a;
}
?>
答案 0 :(得分:4)
$feed_url = 'http://www.myapicallexample.com';
$xml = simplexml_load_file($feed_url);
foreach($xml->hits->hit as $hit){
echo "url:".$hit->d;
}