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!
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
Can you use
Which i get from here http://dynamicweblab.com/2012/09/10-useful-wordpress-functions-to-reduce-your-development-time
Also get more details about this function http://codex.wordpress.org/Function_API/wp_remote_get
Hope this will help
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 onedescription
you don’t need the innerforeach
loop – the following will all have the same effect:foreach ($item->description as $desc) { echo $desc; }
(loop over all child elements with tag namedescription
)echo $item->description[0];
(access the firstdescription
child node specifically)echo $item->description;
(access the first/onlydescription
child node implicitly; this is why you can write$feed->channel->item
and it would still work if there was a secondchannel
element, it would just ignore it)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:
And I got empty description back like this:
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
After making this change, I got description like this: