0

In the http://feeds.feedburner.com/rb286, there are many images. However, when i convert it into and xml object with simplXmlElement, i'm not able to see the images.My code:

if (function_exists("curl_init")){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://feeds.feedburner.com/rb286");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data=curl_exec($ch);
curl_close($ch);
//print_r($data);   //here i'm able to see the images
     $doc=new SimpleXmlElement($data);
     print_r($doc);   //here i'm not able to see the images
  }

Can someone tell me on how can I access the images after converting to xml object? thank you.

1 Answer 1

2

You will have to iterate trough the <content:encoded> tags of the individual <items> in the <channel> main tag. I would use the xpath method to select the tags. Once you get the element you want you can grep the <img> out of them with string manipulation tools like preg_match_all:

Edit: added more refined image tag matching, that excludes ads from feedburner and other cdns.

$xml = simplexml_load_string(file_get_contents("http://feeds.feedburner.com/rb286"));

foreach ($xml->xpath('//item/content:encoded') as $desc) {
    preg_match_all('!(?<imgs><img.+?src=[\'"].*?http://feeds.feedburner.com.+?[\'"].+?>)!m', $desc, $>

    foreach ($m['imgs'] as $img) {
        print $img;
    }
}

The <content:encoded> tag is namespaced, so if you want to use simplexml's built in property mapping, you have to deal with it like this:

// obtain simplexml object of the feed as before
foreach ($xml->channel->item as $item) {
    $namespaces = $item->getNameSpaces(true);
    $content = $item->children($namespaces['content']);
    print $content->encoded; // use it howevery you want
}

You can read more from the xpath query language here.

Sign up to request clarification or add additional context in comments.

10 Comments

why ru using file_get_contents? can I just use $xml=simplexml_load_string($data)? YOur http addr is not the same as mine? is it a typo? mine is feeds.feedburner.com/rb286.
Just to make the snippet shorter, doesn't matter how you obtain the feed, you could also use simplexml_load_file('http://.../'), too.
Oh, the '?l' was a typo, however doesn't matter of in the result. The original feedburner url gives you different result for your browser then when you call it with curl/php. To get to the xml feed with the browser follow this url: feeds.feedburner.com/rb286?format=xml .
is it better not to use curl? now i tried using simpleXml_load_file("http//feeds.feedburner.com/rb286?format=xml"). I still don't see any <content:encoded> tag. Why i'm not able to see that tag? However, the images are displaying. Please help....i'm new to xml, xpath, rss feed. Thank you.
Well, not by default because of namespacing, i've updated the example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.