2

这是我第一次在这里提问,我也是PHP和简单XML的新手,但我决心学习这个。我已经搜索了之前提出的所有相关问题,但在将其应用于我的情况时遇到了困难,尽管它看起来很相似。真令人沮丧:(。

我正在尝试从 Amazon Cloudsearch 以 XML 格式读取 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;
}



?>
4

1 回答 1

4
$feed_url = 'http://www.myapicallexample.com';
$xml = simplexml_load_file($feed_url);

foreach($xml->hits->hit as $hit){
    echo "url:".$hit->d;
}
于 2013-01-26T01:08:32.087 回答