SimpleXML feed showing blank arrays – how do I get the content out?

I’m trying to get the image out of a rss feed using a simpleXML feed and parsing the data out via an array and back into the foreach loop…

in the source code the array for [description] is shown as blank though I’ve managed to pull it out using another loop, however, I can’t for the life of me work out how to pull in the next array, and subsequently the image for each post!

Read More

help?

you can view my progress here: http://dev.thebarnagency.co.uk/tfolphp.php

here’s the original feed: feed://feeds.feedburner.com/TheFutureOfLuxury?format=xml

$xml_feed_url = 'http://feeds.feedburner.com/TheFutureOfLuxury?format=xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);

function produce_XML_object_tree($raw_XML) {
libxml_use_internal_errors(true);
try {
    $xmlTree = new SimpleXMLElement($raw_XML);
} catch (Exception $e) {
    // Something went wrong.
    $error_message = 'SimpleXMLElement threw an exception.';
    foreach(libxml_get_errors() as $error_line) {
        $error_message .= "t" . $error_line->message;
    }
    trigger_error($error_message);
    return false;
}
return $xmlTree;
}

$feed = produce_XML_object_tree($xml);

print_r($feed);

foreach ($feed->channel->item as $item) {
// $desc = $item->description;

echo '<a href="'.$item->link.'">link</a><br>';

    foreach ($item->description as $desc) {
        echo $desc;`


    } 
}

thanks

Related posts

Leave a Reply

3 comments

  1. I’m not entirely clear what your problem is here – the code you provided appears to work fine.

    You mention “the image for each post”, but I can’t see any images specifically labelled in the XML. What I can see is that inside the HTML in the content node of the XML, there is often an <img> tag. As far as the XML document is concerned, this entire blob of HTML is just one string delimited with the special tokens <![CDATA[ and ]]>. If you get this string into a PHP variable (using (string)$item->content you can then find a way of extracting the <img> tag from inside it – but note that the HTML is unlikely to be valid XML.

    The other thing to mention is that SimpleXML is not, as you repeatedly refer to it, an array – it is an object, and a particularly magic one at that. Everything you do to the SimpleXML object – foreach ( $nodeList as $node ), isset($node), count($nodeList), $node->childNode, $node['attribute'], etc – is actually a function call, often returning another SimpleXML object. It’s designed for convenience, so in many cases writing what seems natural will be more helpful than inspecting the object.

    For instance, since each item has only one description you don’t need the inner foreach loop – the following will all have the same effect:

    • foreach ($item->description as $desc) { echo $desc; } (loop over all child elements with tag name description)
    • echo $item->description[0]; (access the first description child node specifically)
    • echo $item->description; (access the first/only description child node implicitly; this is why you can write $feed->channel->item and it would still work if there was a second channel element, it would just ignore it)
  2. I had an issue where simplexml_load_file was returning some array sections blank as well, even though they contained data when you view the source url directly.

    Turns out the data was there, but it was CDATA so it was not properly being displayed.

    Is this perhaps the same issue op was having?

    Anyways my solution was this:

    So initially I used this:

    $feed = simplexml_load_file($rss_url);
    

    And I got empty description back like this:

    [description] => SimpleXMLElement Object
        (
        )
    

    But then I found this solution in comments of PHP.net site, saying I needed to use LIBXML_NOCDATA:
    https://www.php.net/manual/en/function.simplexml-load-file.php

    $feed = simplexml_load_file($rss_url, "SimpleXMLElement", LIBXML_NOCDATA);
    

    After making this change, I got description like this:

    [description] => My description text!