Forum Moderators: coopster
On my news site I have a small script that gathers RSS headlines from several news sources and stores them in my DB.
my code:
// $sources is an array with list of my sources
foreach($sources as $source) {
include('../sources/'.$source.'.php');
}
Now the code runs sources one by one and performs necessary tasks which are defined in each source include.
This works fine, but sometimes a particular source is unavailable or down.
Let's say my first 4 sources run fine and the fifth one is www.examplenews.com which is temporarily unavailable.
My script just hangs trying to connect to it and eventually times out. I've set timeout is set to 420 seconds.
Is there any way I can have PHP skip the particular bad source and proceed with the next one? Let's say if it can't connect within 10 seconds or so?
//At the beginning of the script:
$start = microtime();
//end od script:
//$time_imparted is whatever exec time you agree on
$duration = $time_imparted + microtime();
//then compare $start and $duration
// if it exceeds $time_imparted then break (read below)
set your four RSSs as an array
create a switch and use BREAK() [php.net]
review the second example from the top first demo
<edit>second thoughts</edit>
Not sure the switch will resume if it breaks
as such you could in addition envision a few more IF
if it breaks in position 2 you know that 1 is OK so reload 3 & 4 etc..
see where I go ...
the whole thing is quite heavy I'll love to see a better solution.
<edit>more to it</edit>
Or create each RSSs as a function, then use exit()
exiting a function does not stop the whole script
you may even simplify it by creating that concept as a class.