Jump to content

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/254919-unstable-remote-site-avoid-timeout-etc/
Share on other sites

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...";
}

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?

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;
}
?>

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.