Purple Butterfly
09-20-2005, 12:27 AM
Hello, I'm receiving incoming RSS on a web page and whenever 1 feed goes down the whole page is unable to be displayed. I receive the message "http://www.sitename.com/could not be opened".
How can I isolate this so that even if certain feeds go down the page will still show. A working example of this sis located here
http://users.cs.cf.ac.uk/T.A.Jackson/Css/ on the left hand side there is a box and if the feed is not open the page still loads.
I can't afford to have pages go down because of 1 or 2 feeds dropping connection. TIA to everyone who decides to help.
If it matters this is all being done in php.
TechSolution
09-20-2005, 07:43 PM
How are you receiving the RSS feed? Try trapping the error by double checking everything before you precede to the next step in the process.
Could you post the section of your code responsible for the feed?
Purple Butterfly
09-21-2005, 12:04 AM
Here's the entire code. This is at the top of the web page before the declarations
<?php
$itemNum=0;
class RSSParser {
var $channel_title="";
var $channel_website="";
var $channel_description="";
var $channel_pubDate="";
var $channel_lastUpdated="";
var $channel_copyright="";
var $title="";
var $link="";
var $description="";
var $pubDate="";
var $author="";
var $url="";
var $width="";
var $height="";
var $inside_tag=false;
function RSSParser($file) {
$this->xml_parser = xml_parser_create();
xml_set_object( $this->xml_parser, $this );
xml_set_element_handler( $this->xml_parser, "startElement", "endElement" );
xml_set_character_data_handler( $this->xml_parser, "characterData" );
$fp = @fopen("$file","r") or die( "$file could not be opened" );
while ($data = fread($fp, 4096)){xml_parse( $this->xml_parser, $data, feof($fp)) or die( "XML error");}
fclose($fp);
xml_parser_free( $this->xml_parser );
}
function startElement($parser,$tag,$attributes=''){
$this->current_tag=$tag;
if($this->current_tag=="ITEM" || $this->current_tag=="IMAGE"){
$this->inside_tag=true;
$this->description="";
$this->link="";
$this->title="";
$this->pubDate="";
}
}
function endElement($parser, $tag){
switch($tag){
case "ITEM":
$this->titles[]=trim($this->title);
$this->links[]=trim($this->link);
$this->descriptions[]=trim($this->description);
$this->pubDates[]=trim($this->pubDate);
$this->authors[]=trim($this->author);
$this->author=""; $this->inside_tag=false;
break;
case "IMAGE":
$this->channel_image="<img src=\"".trim($this->url)."\" width=\"".trim($this->width)."\" height=\"".trim($this->height)."\" alt=\"".trim($this->title)."\" border=\"0\" title=\"".trim($this->title)."\" />";
$this->title=""; $this->inside_tag=false;
default:
break;
}
}
function characterData($parser,$data){
if($this->inside_tag){
switch($this->current_tag){
case "TITLE":
$this->title.=$data; break;
case "DESCRIPTION":
$this->description.=$data; break;
case "LINK":
$this->link.=$data; break;
case "URL":
$this->url.=$data; break;
case "WIDTH":
$this->width.=$data; break;
case "HEIGHT":
$this->height.=$data; break;
case "PUBDATE":
$this->pubDate.=$data; break;
case "AUTHOR":
$this->author.=$data; break;
default: break;
}//end switch
}else{
switch($this->current_tag){
case "DESCRIPTION":
$this->channel_description.=$data; break;
case "TITLE":
$this->channel_title.=$data; break;
case "LINK":
$this->channel_website.=$data; break;
case "COPYRIGHT":
$this->channel_copyright.=$data; break;
case "PUBDATE":
$this->channel_pubDate.=$data; break;
case "LASTBUILDDATE":
$this->channel_lastUpdated.=$data; break;
default:
break;
}
}
}
}
$bbcfrontpg = new RSSParser("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml#");
Tha last line is the link to the feed. Thanks a lot.
Content-Type.com
09-22-2005, 12:13 AM
Originally posted by Purple Butterfly
Hello, I'm receiving incoming RSS on a web page and whenever 1 feed goes down the whole page is unable to be displayed. I receive the message "http://www.sitename.com/could not be opened".
How can I isolate this so that even if certain feeds go down the page will still show. A working example of this sis located here
http://users.cs.cf.ac.uk/T.A.Jackson/Css/ on the left hand side there is a box and if the feed is not open the page still loads.
I can't afford to have pages go down because of 1 or 2 feeds dropping connection. TIA to everyone who decides to help.
If it matters this is all being done in php.
The best way to handle feeds is not to read them from the source on every request. Setup a cron that runs every hour and store the results on your local web server. Then in your actual website, simply open up your local cached version.
That way you never have to worry about the 3rd party feed going down. No matter what it will always open your local copy and if their feed is down when your cron script attempts to get it; it will simply exit and not try again until its next runtime.
Purple Butterfly
09-22-2005, 10:55 AM
Thanks for the idea of the cron , never thought of that . Not quite sure how it would be set up either. I do still want another option besides the ones I have on line 24, and 25
$fp = @fopen("$file","r") or die( "$file could not be opened" );
while ($data = fread($fp, 4096)){xml_parse( $this->xml_parser, $data, feof($fp)) or die( "XML error");}
What else could be done. I was thinking maybe something like if one feed dies then it displays the next in line or maybe nothing but I still want the others to continue... I'm drawing a blank.
As it stands right now one dies they all die, so even with the cache setup it's still creates a hangup. The cron job hides it from the end user which is good, but the problem is still there for me on the back end. Sometimes, the feeds have had problems for days on end, which can create a real nuisance.
Thanks again, I'm just not able to wrap my head around this for some reason.
TechSolution
09-22-2005, 05:01 PM
Don't use the "or die" syntax. The die syntax combines echo with exit.
Try this:
$fp = @fopen($file, 'r');
if ($fp !== false) {
// run loop code, etc
} else {
// output error message
}
(You don't need to put quotes around the variables "$file" in your arguments, by the way.