PHP and XML: Parsing RSS 1.0

i have a question about the xml/rss parser you wrote an article on at sitepoint.com. sometimes in the description tag the publisher doesn’t encapsulate the content in CDATA arrays. if they then use certain html tags those tags are also xml tags and the description comes out blank. namely if the very first tag in the description is a div tag; so basically the whole description is in a div tag and the parser returns an empty description. know what i mean? how can i handle this case?

Great Article! One of the best on this subject.

is there any way of assigning classes to the html thats outputted by this script, in order to style the resulting text? i found that this gave me a parse error

printf(“<dd class=“foo”>%s</dd>”,htmlspecialchars(trim($this->description)));

Very timely! I have been looking for a replacement for the excellent MagpieRSS, and the only real option at this stage seems to be writing our own RSS reader.

is there any way of assigning classes to the html thats outputted by this script, in order to style the resulting text? i found that this gave me a parse error

printf(“<dd class=“foo”>%s</dd>”,htmlspecialchars(trim($this->description)));

Posted by: stef Jul 12th, 2005 @ 3:00 AM MDT


printf(“<dd class=‘foo’>%s</dd>”,htmlspecialchars(trim($this->description)));

Should work. You put " inside the " for the printf.

is there any way to do this with out using the fopen function?

I’d also like to know how to replace the fopen function here. I’ve read that the ‘curl’ library can be used, or fsocketopen, however I have no idea how to replace the fopen function with one of these methods. Any advice?

This article worked perfectly for me with fopen enabled. But my webhost has fopen disabled. I had to plug in the following code to get it to work without using fopen:

 $ch = curl_init();
  curl_setopt( $ch, CURLOPT_URL, "http://www.example.com/headlines.jsp" );
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  $data = curl_exec( $ch );
  if( empty( $data )) die("Error reading RSS data.");
  
  xml_parse($xml_parser, $data ) or 
      die(sprintf("XML error: %s at line %d",
          xml_error_string(xml_get_error_code($xml_parser)), 
          xml_get_current_line_number($xml_parser)));
  
  curl_close($ch);

can you assign a timeout using the curl() method?

thanks

I’ve tried your code, and PHP cookbook code which is similar and both throw “Error reading RSS data”.

Do I need to do something to my ini settings?

my allow_url_fopen is disabled - must be the problem

I’ve found another PHP parser and would like to try accessing SitePoint feeds. It seems to be taking a parameter of filename.rss. I can’t find anything on the feeds pages that references those files. Am I going down the the wrong path looking for an rss file?

Thanks
Rich

Never mind. I had the rdf extention marked wrong. Works like a charm.

Hi, I would like to know if there is any parser available to parse the rss feeds using just javascript/dom - ie, no php in between?

Another excellent article made by Kevin. Keep them going:)

Great Article

Can someone update it for rss 2.0

Just tried this and I am getting an error:
Warning: fopen(http://www.sitepoint.com/rss.php): failed to open stream: Bad file descriptor in <filename>

But when I reference the file locally it works fine. Any idea what this is about?

Juan
I think to limit the number of results, try replacing the r in the following with limit=5 or what ever number of results you want.
$fp = fopen(“http://www.sitepoint.com/rss.php","r”)

Is it possible to limit the feed’s number?

I’m ‘asking’ a rss server but it send too many feeds. Can I limit taht number?

Thanks.