visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Web :: General :: RSS feed parser problem


By: Nono Great Britain  Date: 24/03/2007 09:37:16  English  Points: 20 Status: Answered
Quality : Excellent
I'm having some trouble with my RSS feed parser. It probably is bad code

If a feed is not available, my catch doesn't catch it and the feed is still parsed causing my homepage to fall over.

Can you take a look and see what I can do to make it better so its less harsh on my server...

<?php function startElement($parser,$name,$attrs) { global $rss_channel, $currently_writing, $main; switch($name) { case 'RSS': case 'RDF:RDF': case 'ITEMS': $currently_writing = ''; break; case 'CHANNEL': $main = 'CHANNEL'; break; case 'IMAGE': $main = 'IMAGE'; $rss_channel['IMAGE'] = array(); break; case 'ITEM': $main = 'ITEMS'; break; default: $currently_writing = $name; break; } } function endElement($parser,$name) { global $rss_channel, $currently_writing, $item_counter; $currently_writing = ''; if($name == 'ITEM') { $item_counter++; } } function characterData($parser,$data) { global $rss_channel, $currently_writing, $main, $item_counter; if($currently_writing != '') { switch($main) { case 'CHANNEL': if(isset($rss_channel[$currently_writing])) { $rss_channel[$currently_writing] .= $data; } else { $rss_channel[$currently_writing] = $data; } break; case 'IMAGE': if(isset($rss_channel[$main][$currently_writing])) { $rss_channel[$main][$currently_writing] .= $data; } else { $rss_channel[$main][$currently_writing] = $data; } break; case 'ITEMS': if(isset($rss_channel[$main][$item_counter][$currently_writing])) { $rss_channel[$main][$item_counter][$currently_writing] .= $data; } else { $rss_channel[$main][$item_counter][$currently_writing] = $data; } break; } } } function removeEvilAttributes($tagSource) { $stripAttrib = "' (style|class)=\"(.*?)\"'i"; $tagSource = stripslashes($tagSource); $tagSource = preg_replace($stripAttrib, '', $tagSource); return $tagSource; } function removeEvilTags($source) { $allowedTags = '<p><a></a></p>'; $source = strip_tags($source, $allowedTags); return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source); } set_time_limit(0); $rss_channel = array(); $currently_writing = $main = $file = ''; $file = 'http://localhost/rss.xml'; if(($fp = fopen($file,'r'))) { $item_counter = 0; $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser,'startElement','endElement'); xml_set_character_data_handler($xml_parser,'characterData'); while($data = fread($fp,4096)) { if(!xml_parse($xml_parser,$data,feof($fp))) { die(sprintf('XML error: %s at line %d',xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser); $html .= '<table class="feeds-table">'."\n"; $html .= '<tr>'."\n"; $html .= ' <td >'."\n"; $html .= ' <h2>News Headlines</h2>'."\n"; $html .= ' </td>'."\n"; $html .= '</tr>'."\n"; if(isset($rss_channel['ITEMS'])) { if(count($rss_channel['ITEMS']) > 0) { for($i = 0;$i < 5; $i++) { $html .= '<tr>'."\n"; $html .= ' <td>'."\n"; $title = str_replace("'","&quot;",$rss_channel['ITEMS'][$i]['TITLE']); $description = str_replace("'","&#39;",$rss_channel['ITEMS'][$i]['DESCRIPTION']); $html .= ' <p><a href="'.$rss_channel['ITEMS'][$i]['LINK'].'" onclick="javascript:window.open(this.href);return false;">'.$title.'</a><br/>'.html_entity_decode($description).'</p>'."\n"; $html .= ' </td>'."\n"; $html .= '</tr>'."\n"; } } else { $html .= '<tr><td><p>there are no articles in this feed.</p></td></tr>'; } } $html .= '</table>'; } ?>

By: VGR Date: 24/03/2007 09:44:25 English  Type : Answer
probably because you should write this ?

$fp =@fopen($file,'r'); if ($fp!==FALSE) { }


By: OpConsole Date: 04/07/2007 16:50:00 English  Type : Comment
Dear,

If you found some of the above comments to have proved helpful in solving your issue, you shall Accept the Answer or sPlit points between the various useful comments. Each one can receive a quality evaluation from + (somewhat helpful) to +++ (working solution).

Given this Question has been Open for quite a while now, please accordingly "accept an Answer" ASAP

This Question will be forced-closed in one month from now.

Thanks and regards.

Admin.

Do register to be able to answer

EContact
browser fav
page generated in 94.949010 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page