在PHP中,我正在打开一个流,对其进行写入,然后从中读取。我想在读取流时设置超时,但是无论我将其设置为多低(0微秒,10微秒),元数据都永远不会显示“timed_out”!
相关代码:
//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 );
}
}
}
我究竟做错了什么?
最佳答案
我发现的 key 是stream_set_blocking($fp, TRUE )
。
如果是FALSE
,那么$status['timed_out']
似乎没有任何实际效果。 TRUE
[PHP默认]有效。
关于php - stream_set_timeout永远不会工作,无论超时多低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13424989/