2

ここで質問するのはこれが初めてであり、PHPと単純なXMLの初心者でもありますが、これを学ぶことを決意しています。以前に尋ねられたすべての関連する質問を検索しましたが、非常に似ているように見えますが、私の状況に適用するのに問題があります。本当にイライラする:(。

AmazonCloudsearchからのAPI応答をXML形式で読み取ろうとしています。

元の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ステートメントのさまざまな組み合わせを試しましたが、何も機能しません。コンピューターを窓の外に投げ出す準備ができました。前もって感謝します!

補遺:これは私の以前の多くの骨の折れる試みの1つの例です:)

<?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 に答える