1

I tried to parse the link on rss feed with images using php.

I tried parsing the rss feed to display images from the enclosure tag. I tried the solution given on that link. But it doesn't work. The element_attributes function has not been defined.

So, I tried to get the images using xPath. The following is my output (empty array):

Array()

on my web server error log.

Can anyone point out on where i'm going wrong? Thank you.

<?php
if (function_exists("curl_init")){
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL, 'http://menmedia.co.uk/manchestereveningnews/news/rss.xml');
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    //curl_setopt($ch,CURLOPT_HEADER,0);
$data=curl_exec($ch);


curl_close($ch);


$doc=new SimpleXmlElement($data,LIBXML_NOCDATA);

function getImage($xml){
    $imgs=$xml->xPath('rss/channel/enclosure[@attributes]');
    print_r($imgs);
    /*foreach ($imgs as $image){
        echo $image->url;
    }*/
}
if (isset($doc->channel)) getImage($doc);

} ?>

3
  • You have forgotten to provide the source XML document. Please, edit the question and provide the missing information. Commented Jul 14, 2012 at 19:33
  • First i'm downloading the RSS feed ie. $data, then $doc is the SimpleXML element object. Commented Jul 14, 2012 at 23:28
  • vaanipala: You must show us the actual document that you are evaluating the XPath expressions on. Nobody can specify an XPath expression that must select specific nodes, if the XML document isn't known and it isn't known where exactly in the document these nodes are. Commented Jul 15, 2012 at 3:21

3 Answers 3

1
$imgs=$xml->xPath('//channel/item/enclosure');
foreach($imgs as $img) {
    var_dump($img->attributes());
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Attributes are displaying fine. Now I have to figure out on how to access the images. :)
1

I got it to work. It is displaying all the images. As Dimitre has suggested, i'm using '/rss/channel/item/enclosure' minus '@*'. The following is my function:

function getImage($xml){
    $encls=$xml->xPath('/rss/channel/item/enclosure'); 
        foreach($encls as $encl) { 
            print_r('<img src='.$encl->attributes()->url.'/>');
            echo('<br>');
            }

}//end function getImage

thank you guys!

Comments

1

This single XPath expression:

/rss/channel/item/enclosure/@*

Selects all attributes of all enclosure elements that are children of all channel elements that are children of the top rss element.

This is both simpler and much more efficient thatn using:

//channel/item/enclosure

and then finding the attributes in a separate evaluation.

Do remember:

Evaluation of an XPath expression starting with the // pseudo-operator is usually much slower than using a specific path -- because this causes a non-optimizing XPath processor to traverse the whole document.

3 Comments

+1 - I'm sure you meant to say "all attributes of all enclosure elements".
i tried Dimitre Novatchev's solution. But, i'm getting all NULLs as output.
@vaanipala: Either the elements are in a default namespace (which isn't so with the document that the provided links points to, or you had a copy/paste error, or you are using a non-compliant (and buggy) XPath engine. Another possibility is that you are evaluating the XPAth expression against a different document from the one you pointed us to.

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.