繁体   English   中英

使用PHP foreach循环从AWS Cloudsearch API响应中解析简单XML结果

[英]Using PHP foreach loop to parse Simple XML result from AWS Cloudsearch API response

这是我第一次在这里提出问题,我也是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;
}



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

foreach($xml->hits->hit as $hit){
    echo "url:".$hit->d;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM