How to time-out a slow getFeed

A client requires the results of a small, external RSS feed to be echoed on his pages. I was given a simple script that does that nicely (this is slightly modified for public viewing):


<?php
			
		function getFeed($feed_url) {
			
			$content = file_get_contents($feed_url);
			
			$long = array("Platinum", "Palladium", "Rhodium", "Iridium", "Ruthenium", "per troy oz", "US$");
			$short = array("Pt:", "Pd:", "Rh:", "Ir:", "Ru:", "", "");
			$cleanxml = str_replace($long, $short, $content);
			
			$xml = new SimpleXmlElement($cleanxml);
			
			foreach($xml->channel->item as $entry) {
				echo "<span class='price'>" . $entry->title . "" . $entry->description . "</span>";
				}
			}

			getFeed("http://www.example.com/rss/feed.xml");
					
		?>

It works fine, except that the page load seems to hang sometimes and the site crashes. I think the problem is that there is a very slow response from the remote server at times. So, I’ve been looking for some solutions. One thought was to set up a cron job to retrieve the data ahead of time. But I’m wondering if there is a simpler PHP solution. For example, is it possible to set a timeout on the above operation, such that, if the feed takes more than a few seconds to arrive, a kind of N/A result is returned? E.g.


} else {

echo "<span class='price'>Pt: N/A </span><span class='price'>Pd: N/A </span><span class='price'>Rh: N/A </span><span class='price'>Ir: N/A </span><span class='price'>Ru: N/A </span>";
}

That might at least stop the site from crashing. I’ve tried to play around with setting a timeout, but I’m just stabbing in the dark, as the PHP manual is way over my head at this stage. If I have to, I’ll just go back to the developer and ask for an update, but as I’m trying to learn a bit of PHP, I though I might ask here first. :slight_smile:

Is the idea of a timeout even a viable idea? Or would a cron job be better?

You can set timeout using cURL not file_get_contents. For that reason it is best to avoid file_get_contents for anything other than files on the same server. Use cURL library functions instead to make http requests to external sources.

Cool, thanks for the tip. Not being familiar with cURL, I’ve done a bit of reading around and tried a few things I’ve found on the web, but it led to nothing—a blank page. I really have no idea what I’ve doing with it. I might have to go back to the developer on this one. It was worth a shot, though. My beginner PHP book doesn’t mention cURL at all. :slight_smile:

Checkout The first example here.

The option your looking to set is CURLOPT_TIMEOUT. It is on that page so search for that string (no id to link directly to it).

da manual knows all!

Thanks oddz. Unfortunately, I have no idea how to merge that code with what I posted above.

Yes, no doubt. Pity it’s in a different language, though. :lol:

EDIT: one problem might be that cURL is not available on my server. It doesn’t show up in PHP info. :frowning:

EDIT2: I found out how to install it. Now I’m getting somewhere. Something actually happens on the page now. :slight_smile: