In PHP, I'm opening a stream, writing to it and then reading from it. I want to set a timeout on the reading of the stream but no matter how low I set this (0 microseconds, 10 microseconds) the meta data never shows "timed_out"!
Relevant code:
//open the socket
if ( $fp = fsockopen( gethostbyname(host), port, $errno, $errstr, $timeout ) ) {
//Send command to the host
if ( fwrite( $fp, $requestCommand ) ) {
//Set timeout and blocking
stream_set_blocking( $fp, FALSE );
stream_set_timeout( $fp, 0, 10 );
//Check for timeout
$info = stream_get_meta_data( $fp );
echo $info[ 'timed_out' ];
//Read and check for timeout
while ( !$info['timed_out'] && !feof( $fp ) ) {
$response .= fread( $fp, 4096 );
//Get meta data (which has timeout info)
$info = stream_get_meta_data( $fp );
}
}
}
What am I doing wrong?
The key I found is the stream_set_blocking($fp, TRUE )
.
If FALSE
, then $status['timed_out']
seems to not have any practical effect. TRUE
[PHP default] works.