简体   繁体   中英

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

This is my first time asking a question here, and I am also a novice at PHP and simple XML, but am determined to learn this. I've searched through all of the relevant questions that have been asked before but am having trouble applying it to my situation although it looks so similar. Really frustrating :(.

I am trying to read an API response from Amazon Cloudsearch in XML format.

Here is the original XML response:

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

I've used SimpleXML to start:

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
            )

    )

)

I am trying to read and display the images for each item shown based on the URL provided in the result. I know I should be using a "foreach" loop here but I cant seem to get it right, so here is the long way I've done it:

<?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>";
?>

I've tried various combinations of the foreach statement from looking at answers to other questions but nothing works. I'm ready to throw my computer out the window. Thanks in advance!

Addendum: Here is an example of one of my many prior boneheaded attempts :)

<?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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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