php stream_get_contents,PHP: stream_get_contents - Manual

/*

* problem: stream_get_contents blocks / is very slow.

* I have tried

* 1: stream_set_blocking, doesn't make a difference.

* 2: stream_get_meta_data['unread_bytes'] = ITS BUGGED, ALWAYS SAYS 0.

* 3: feof(): ALSO EFFING BLOCKING

* 4: my_stream_get_contents hack... kinda working! :D

*/

function my_stream_get_contents ($handle, $timeout_seconds = 0.5)

{

$ret = "";

// feof ALSO BLOCKS:

// while(!feof($handle)){$ret.=stream_get_contents($handle,1);}

while (true) {

$starttime = microtime(true);

$new = stream_get_contents($handle, 1);

$endtime = microtime(true);

if (is_string($new) && strlen($new) >= 1) {

$ret .= $new;

}

$time_used = $endtime - $starttime;

// var_dump('time_used:',$time_used);

if (($time_used >= $timeout_seconds) || ! is_string($new) ||

(is_string($new) && strlen($new) < 1)) {

break;

}

}

return $ret;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值