note 50172 deleted from function.stream-set-timeout by googleguy

From: Date: Tue, 22 Oct 2013 10:15:16 +0000
Subject: note 50172 deleted from function.stream-set-timeout by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196942@lists.php.net to get a copy of this message
Note Submitter: ridera ---- [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_stuff\n; //the html header, etc. ob_flush(); //makes it echo immediately $delay= 20; //tweak this, seconds $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 = "Finished\n"; $report .= " </body>\n</html>"; echo($report); ?> The second code block calls test.php with the usual "fopen()" <?php $fp= fopen("http://URL/.../test.php", 'rb'); $query_timeout= 4; //tweek this $monitor_time_sec= 120; //master timeout stream_set_blocking($fp, FALSE ); //THIS IS IMPORTANT stream_set_timeout($fp, $query_timeout); $status = socket_get_status($fp); // fetch data from test.php 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; } //end if sleep(2); $status = socket_get_status($fp); } //end while, fetching data fclose($fp); $pq_array['connection_timed_out'] = ($status['timed_out'])? true : false; print_r($pq_array); echo $html_str; //or whatever. ?>

« previous php.notes (#196942) next »