[WHOOPS! sorry had the key point reversed in my text. ]
I have been trying to understand how to use stream_set_timeout when calling a remote http page and put together the following code snippets. The first one is a simple test file "test.php" that is called as an html webpage.
The key I found is the "stream_set_blocking($fp, TRUE )". If "FALSE", then $status['timed_out'] seems to not have any practical effect. "TRUE" [PHP default] works.
Note, I have two timeouts, stream and monitor. I need both in my application.
<?php
echo $html_stuffn; ob_flush(); $delay= 20; $report = "<div>Test started at: " . date("H:i:s")</div>n";
$report .= "<div>Started delay= $delay)</div>n";
echo($report);
ob_flush();
$i=1;
$start_time= time();
while($i <= 10){
$diff= time()-$start_time;
$msg = $i . " at " . $diff;
echo "$msg<br>n";
sleep($delay);
$i= $i+1;
} // end while
$report = "Finishedn";
$report .= " </body>n</html>";
echo($report);
?>
The second code block calls test.php with the usual "fopen()"
<?php
$fp= fopen("http:$query_timeout= 4; $monitor_time_sec= 120; stream_set_blocking($fp, FALSE ); stream_set_timeout($fp, $query_timeout);
$status = socket_get_status($fp);
while (!feof($fp) && !$status['timed_out']) {
$chunk = fread($fp, 10000);
$length = strlen($chunk);
$html_str .= $chunk;
$diff = time() - $start_time;
$tm = $status['timed_out'];
echo "<div>At $diff seconds >> $length bytes read, Status[timed out]: ($tm)</div>";
ob_flush();
if ($diff > $monitor_time_sec) {
$pq_array['monitor_timed_out'] = true;
break;
} sleep(2);
$status = socket_get_status($fp);
} fclose($fp);
$pq_array['connection_timed_out'] = ($status['timed_out'])? true : false;
print_r($pq_array);
echo $html_str; ?>