phpphp-stream-wrappers

PHP fread catch stream timeout happened


I use the PHP function fread to read the data from a stream opened with

$fh=fopen('http://.....');

i set the timeout for the stream with

socket_set_timeout($fh,10);

if a timeout happened during the fread execution then can i somehow to know about this?

$contents = fread($fh, 1024);

if timeout happens then will $contents be equal empty string or FALSE ? how to know that timeout happened? is there a way?


Solution

  • According to the doc page for that function:

    When the stream times out, the 'timed_out' key of the array returned by stream_get_meta_data() is set to TRUE, although no error/warning is generated.

    So there are no errors/warnings generated, but inspecting the output from stream_get_meta_data will give you a clue.