/*
* 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;
}