Re: simpleXML - simplexml_load_file() timeout?

From: Date: Tue, 11 Apr 2006 02:54:21 +0000
Subject: Re: simpleXML - simplexml_load_file() timeout?
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-233726@lists.php.net to get a copy of this message
Richard Lynch wrote:
On Mon, April 10, 2006 6:17 pm, Rasmus Lerdorf wrote:
Martin Alterisio wrote:
Maybe you can read the contents of the feeds using fsockopen() and stream_set_timeout() to adjust the timeout, or stream_set_blocking() to read it asynchronously, and then load the xml with simplexml_load_string(). PS: I forgot to reply to all and mention you'll have to send the GET http command and headers, check the php manual for examples.
You can just use fopen() to avoid all that.
No, he can't. Sorry, Rasmus. :-) If the URL is not working at the time of fopen() then you'll sit there spinning your wheels waiting for fopen() itself to timeout, before you ever GET a valid file handle to which one can apply stream_set_timeout and/or stream_set_blocking.
I thought it was the case of an overloaded slow-responding server, not one that is down. For the initial connection just set your default_socket_timeout appropriately if you are not happy with the default. So you just add 1 line to my example: function request_cache($url, $dest_file, $ctimeout=60, $rtimeout=5) { if(!file_exists($dest_file) || filemtime($dest_file) < (time()-$ctimeout)) {
    ini_set('default_socket_timeout',$rtimeout);
    $stream = fopen($url,'r');
    stream_set_blocking($stream,true);
    stream_set_timeout($stream, $rtimeout);
    $tmpf = tempnam('/tmp','YWS');
    file_put_contents($tmpf, $stream);
    fclose($stream);
    rename($tmpf, $dest_file);
} } Problem solved. -Rasmus

« previous php.general (#233726) next »