tags:

views:

15

answers:

1

I've got a SimpleXMLElement for which I read in a XML file using curl. Trying to get the values out of it I seem to need to use a nested foreach, but in a similar situation I was able to do this in one go. Now I'm really curious why this isn't working:

Its the delicious RSS feed;

SimpleXMLElement Object
(
    [title] => Delicious/chrisvdberge
    [link] => http://www.delicious.com/chrisvdberge
    [description] => bookmarks posted by chrisvdberge
    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [title] => Diagrammr
                    [pubDate] => Thu, 06 May 2010 12:40:07 +0000
                    [guid] => http://www.delicious.com/url/2fc2ed0e870ec9d001b645dfaed0e771#chrisvdberge
                    [link] => http://www.diagrammr.com/
                    [comments] => http://www.delicious.com/url/2fc2ed0e870ec9d001b645dfaed0e771
                    [source] => chrisvdberge's bookmarks
                    [category] => Array
                        (
                            [0] => tagthis
                            [1] => visualization
                            [2] => diagrams
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [title] => Ignite Toronto 2: Ryan Coleman - Designing for visual efficiency on Vimeo
                    [pubDate] => Mon, 18 Jan 2010 08:05:06 +0000
                    [guid] => http://www.delicious.com/url/9a0de307f3f37baaa25ad20b4dbc4537#chrisvdberge
                    [link] => http://vimeo.com/8317770
                    [comments] => http://www.delicious.com/url/9a0de307f3f37baaa25ad20b4dbc4537
                    [source] => chrisvdberge's bookmarks
                    [category] => Array
                        (
                            [0] => tagthis
                            [1] => vizthink
                        )

                )

etc.

I can access all the links by using a nested foreach:

foreach ($delicious_res as $item) {
    foreach ($item->item as $entry){
        echo $entry->link."<br />";
    }
}

But I don't understand why I cant use this (which I can in another situation)

foreach ($delicious_res->item as $item) {
        echo $entry->link."<br />";
}

I really like to understand why in order to get a better understanding of SimpleXML in general ;)

A: 

You have to use:

foreach($delicious_res->channel->item as $item) {
    print_r($item);
}

Because the collection of items is inside the <rss> and <channel> tag (the <rss> tag can be omitted as it is the root node of the document).

On another note: if you are not doing anything fancy, you probably don´t need to use curl to fetch the rss feeds xml content. Just use simplexml_load_file with the url as an argument:

$delicious_res = simplexml_load_file('http://path-to-your/file.xml"');
Max
thx a lot! what would be 'fancy' stuff for which Id need curl? and is the simplexml_load_file faster?
Chrisvdberge
@Chrisvdberge Well, fancy stuff would be e. g. using a POST request to fetch the data. When using POST, you´d have to use curl. Curl is normally used in order to do more complex http requests, in your case you could do it much simpler. The performance should be about the same, but the code is much nicer to read instead of the many curl_setopt() calls. :-)
Max