百科狗-知识改变命运!
--

stream_set_timeout() - php 流数据函数stream

百变鹏仔1年前 (2023-11-21)阅读数 40#技术干货
文章标签注释

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]): bool

Sets 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.

参数

$stream

The target stream.

$seconds

stream_set_timeout() - php 流数据函数stream

The seconds part of the timeout to be set.

$microseconds

The 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

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)