One of my websites recently had problems with the PHP news parser.
For many years, it worked fine. However, a while back it started acting up. See the attached image of a screenshot.

And since the PHP parser was the first block of code on the page, since it didn't work, the whole page didn't work.

Now I am trying to narrow down the problem. I am not good with PHP.
The hosting company's Tech Support guy told me the code is bad, even though I made no changes. (Why would it work fine for years and then randomly stop?!?!)
Somebody else suggested the problem might be with FeedBurner, because the feed is coming through FeedBurner.

The error message is, "XML error: not well-formed (invalid token) at line 5"

Does this information indicate the problem is with the PHP code, or the incoming feed?

Any direction/advice/guidance is appreciated.

Does this information indicate the problem is with the PHP code, or the incoming feed?

It seems related to the XML document, not to PHP, otherwise the error would be at PHP level. Could you share the XML?

The XML looks fine to me, it validates and I can parse it in PHP.
They probably made a change to the structure of their XML at some point and it caused your code to break.

You will probably need to share your PHP that you're using to get much further.

It is the Space Exploration feed from Science Daily:

No, I was asking for your RSS feed not for the source but yes, at this point, as suggested by pixelsoul share also your PHP code.

Okay, attached is a text file that includes the section of PHP code for the parser. It has worked fine for years, but if you have any suggestions for making it fail gracefully, it would be appreciated. That way, the whole page wouldn't fail just because the RSS feed is failing.

Heh, I just came to the same solution, correct link is:

The problem happens because the old link asks for a session cookie, which is not submitted by the request, in absence it will return an HTML page, instead of the XML. You can test it by using file_get_contents():

$file = "";

Besides: at line 73 you're declaring $numItems; without any default value, which means the variable is still undefined, and it will fail if for example you do something like:

echo $a++;


Thanks for the advice.

I will clean that up ASAP.

I am also curious about Line 64: set_time_limit(0);
Is this okay?
If it were changed to another number, would it cause the failures to be handled better (i.e., render rest of page instead of wait forever)?

Changing set_time_limit(0); won't change the way PHP handles errors, it will however stop the processing of this, should it continue going for an extended amount of time.

This is the line that causes your page to stop loading when it runs into an error

die(sprintf("XML error: %s at line %d",

The die() function basically "kills" any further processing of code on the page.
It would be better to use PHP Exceptions with a try/catch, or you could create your own error handler.


Error handler

In addition: you could add a timeout to file() so that if the resource is not reachable, it will stop after a defined number of seconds and emit an E_WARNING message:

$options["http"]  = array(
    "method"    => "GET",
    "header"    => "Content-Type: text/xml; charset=UTF-8\r\n".
                   "Connection: close\r\n",
    "timeout"   => 15,
$context = stream_context_create($options);

if( ! ($fp = fopen($file, "r", FALSE, $context))) {

Thank you both very much for the advice. That seems to have solved the problem.
Will now mark the thread "Solved."