stream_set_timeout() - php 流数据函数stream
stream_set_timeout()
(PHP 4 >= 4.3.0, PHP 5, PHP 7)
Set timeout period on a stream
说明
stream_set_timeout(resource $stream,int $seconds[,int $microseconds= 0]): boolSets the timeout value on$stream, expressed in the sum of$secondsand$microseconds.
When the stream times out, the 'timed_out' key of the array returned bystream_get_meta_data()is set toTRUE
, although no error/warning is generated.
参数
$streamThe target stream.
$secondsThe seconds part of the timeout to be set.
$microsecondsThe microseconds part of the timeout to be set.
返回值
成功时返回TRUE
,或者在失败时返回FALSE
。
范例
Example #1stream_set_timeout()example
注释
Note:This function doesn't work with advanced operations likestream_socket_recvfrom(),usestream_select()with timeout parameter instead.
This function was previously called asset_socket_timeout()and latersocket_set_timeout()but this usage is deprecated.
参见
fsockopen()
打开一个网络连接或者一个Unix套接字连接fopen()
打开文件或者 URL
In case anyone is puzzled, stream_set_timeout DOES NOT work for sockets created with socket_create or socket_accept. Use socket_set_option instead. Instead of: Use:
If the timeout fails, because the server remains completely silent, one may have to add stream_select() to make the timeout work. This may be much more efficient that a non-blocking reading operation. Background: We had issues with a SMTP server that was addresses unencrypted while expecting TLS encryption. The stream_set_timeout() alone did not work as expected and the script hung for an hour or more.
Here is a working example for loops:
I didn't have much luck with the suggestions below (although I likely applied them wrong). Instead, I used stream_context_create() and set an http option for timeout. I fed that context into file_get_contents() and voila! To my desperate friend below: the https transport can also use the http stream context options. I haven't verified this works as I don't have a slow responding ssl to test on. But if you are still stressing, give the below a shot (you may need to modify a bit...) Yes...that is a 5 minute timeout.
Another note alread states that blocking-reads may be an issue, if the counterpart responds very slowly - or not at all. The stream timeout may not work as expected in such a situation. However, php.net provides very little information on how to use non-blocking reading operations. Here's a code sample:
This function seems to have no effect when running as a CLI script, see http://bugs.php.net/bug.php?id=36030
If you are using fsockopen() to create a connection, first going to write into the stream and then waiting for the reply (e.g. simulating HTTP request with some extra headers), then stream_set_timeout() must be set only after the write - if it is before write, it has no effect on the read timeout :-( Noticed at least on PHP/4.3.10
stream_set_timeout() is not suitable for such files as UNIX-devices (/dev/...), i suggest to use select() instead with desirable timeout value - that works well.
I have found it required to add "stream_set_blocking($fp, FALSE )" prior to any fgets(), fread(), etc. to prevent the code from hanging up when remote files are called and the response is slow.
I have found that in order to actually stop the socket from timing out the script, you must call stream_get_meta_data and check for a timeout within the loop reading from the socket. Example:
Just in case someone stumbles into my situation... I was trying to get the microseconds part to work and it just wasn't working as expected. Assuming blocking mode is true and I use I get a 1s delay as expected. However when I do I expect a 500microsecond delay but I get no delays at all. However when i do this I get a 500 microsecond delay which is what I was expecting
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!