Re: simpleXML - simplexml_load_file() timeout?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux