簡體   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