Hi, folks.

I input an RSS news feed on to my site and I use a PHP routine to parse the input. I barely know PHP; basically, I learned just enough to get the feed working.

I would now like to make a small edit to the code and am seeking some help.

Presently, if the XML input stream cannot be opened, the code outputs a message and then dies. Here is the code:

if (!($fp = fopen($file, "r"))) {
   die("could not open XML input");
}

Since this code is one of the first things the page does when loading, if the stream cannot be opened, the rest of the page doesn't display.

What I'd like to do now is to have the rest of the page display properly even when the PHP code encounters an error (the rest of the page is just static HTML).

Any suggestions for an alternative to the "die" approach I am presently using?

Thanks.


David

Well, what would you like to do when the feed is not available? I'm assuming that there is something else that the page will display... You're on the right track, basically (without knowing more about your code), something like:

if ($fp = fopen($file, "r")) {
   //do whatever you want with the feed
} else {
  echo "Feed not available at this time. Enjoy the rest of the page.";
}

echo "here is the rest of the page";

The only* reason people use die() (or _should_ use die) is if an essential part of the site is not available, and you can't do anything without that piece. For example a database driven site without the database is pretty useless, so die-ing is an acceptable means out.

If your site *depends* on reading the feed, then maybe die-ing is a good choice.. But if there's other utility, then do something like the code above.. Without knowing more, it's hard to say...

* Ok, so there are other reasons people use die(), and other ways out of an essential part of the site being unavailable. But it's not common among code I've seen.

you can set a variable when you have a valid file and then refer back to that variable later on in your code.

$haverss = false;
if (!($fp = fopen($file, 'r'))) {
$haverss = true
}
//later on
if ($haverss === true) {
//do something with the rss
}

I havent tried magpie, but have had great success with PEAR, the RSS and XML parser are both great packages, however i didnt want to get too deep with a n00b. No offence MCP =]

Use MagpieRSS. :)

I have heard of Magpie before, and looked at it briefly, but wasn't clear how to use it. I think it is just a "black-box" approach, isn't it? I have styled the output quite a bit and got the impression Magpie didn't allow a lot of customization (font style and size, background colors, number of news headlines to display, etc.). Am I mistaken?


-- David

This article has been dead for over six months. Start a new discussion instead.