How to get fopen to timeout properly

Posted by beagleguy on Stack Overflow See other posts from Stack Overflow or by beagleguy
Published on 2010-03-24T20:40:28Z Indexed on 2010/03/24 20:43 UTC
Read the original article Hit count: 276

Filed under:
|
|

hey all, I have the following snippet of php code

if($fp = fopen($url, 'r')) {
    stream_set_timeout($fp, 1); 
    stream_set_blocking($fp, 0);

}
$info = stream_get_meta_data($fp);

I'd like the request to timeout after 1 second... if I put a sleep(20) in my $url that I'm reading it just waits the whole 20 seconds and never times out. Is there a better way to do timeouts with fopen?

If I use ini_set('default_socket_timeout',2); above that code it times out properly but $info then becomes null so ideally I'd like to use the stream functions.

thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about sockets