Berre Posted January 12, 2012 Share Posted January 12, 2012 I have a remote XML file that I try to parse automatically every x minutes. The problem is that it's a bit unstable, so sometimes file_get_contents() times out, other times SimpleXMLElement() fails. I get a few max execution time exceeded (30 sec) every day. Is it possible to return from a function after x seconds, or similar? How can I avoid this? I am very new to error handling. I have also tried using try, but there are other problems (like execution time), and I might be using try wrong, because it still gives errors. A basic example of what fails: $x = file_get_contents("http://www.example.com/rss.xml"); $r = @new SimpleXMLElement($x); Quote Link to comment https://forums.phpfreaks.com/topic/254919-unstable-remote-site-avoid-timeout-etc/ Share on other sites More sharing options...
joel24 Posted January 13, 2012 Share Posted January 13, 2012 use set_time_limit() and suppress the errors i.e. #15 second time limit set_time_limit(15); #suppress errors and get if ($x = @file_get_contents("http://www.example.com/rss.xml")) { $r = @new SimpleXMLElement($x); } else { $error="Could not fetch contents..."; } Quote Link to comment https://forums.phpfreaks.com/topic/254919-unstable-remote-site-avoid-timeout-etc/#findComment-1307105 Share on other sites More sharing options...
Berre Posted January 13, 2012 Author Share Posted January 13, 2012 Thanks, I will give it a try. However, I don't understand exactly how set_time_limit() works, or what it affects. It doesn't seem like it affects the entire page, so is it the time for the surrounding function? If so, should it always be placed in the beginning of a function? What happens if it's outside a function, etc. The PHP manual says that it sets the "number of seconds a script is allowed to run". Is script basically equal to a function? Quote Link to comment https://forums.phpfreaks.com/topic/254919-unstable-remote-site-avoid-timeout-etc/#findComment-1307110 Share on other sites More sharing options...
joel24 Posted January 13, 2012 Share Posted January 13, 2012 put set_time_limit(15); at the top of your script and it will ensure the script runs for a total of 15 seconds before timing out. To generate a prettier timeout message, use register_shutdown_function and have a global variable to tell whether it was successful or not as the shutdown function will be called regardless... haven't tested this but the logic is there <?php function shutdown_function(){ global $gotFile; if (!$gotFile){ exit("Could not receive file in time!"); } } #15 second time limit set_time_limit(15); #haven't got file yet $gotFile = false; #shutdown function register_shutdown_function(‘shutdown_function’); #suppress errors and get if ($x = @file_get_contents("http://www.example.com/rss.xml")) { $r = @new SimpleXMLElement($x); $gotFile=true; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254919-unstable-remote-site-avoid-timeout-etc/#findComment-1307113 Share on other sites More sharing options...
kicken Posted January 13, 2012 Share Posted January 13, 2012 HTTP Context Options - You can set the timeout option in the context to control how long php will wait on the resource. Then just handle errors properly. <?php $ctx = stream_context_create(array( 'http' => array( 'timeout' => 5.0 //wait 5 seconds max ) )); $xml = @file_get_contents($url, false, $ctx); if ($xml===false){ $error="Could not fetch contents..."; } else { $r = new SimpleXMLElement($xml); } The PHP manual says that it sets the "number of seconds a script is allowed to run". Is script basically equal to a function? No, script means the whole script. If the script timeout (set via set_time_limit or in php.ini) is exceeded the script is killed with a fatal error. Quote Link to comment https://forums.phpfreaks.com/topic/254919-unstable-remote-site-avoid-timeout-etc/#findComment-1307115 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.