Web :: General :: RSS feed parser problem |
|||
| By: Nono |
Date: 24/03/2007 09:37:16 |
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("'",""",$rss_channel['ITEMS'][$i]['TITLE']); $description = str_replace("'","'",$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 | Type : Answer |
|
| probably because you should write this ? $fp =@fopen($file,'r'); if ($fp!==FALSE) { } |
|||
| By: OpConsole | Date: 04/07/2007 16:50:00 | 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 |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








