note 50172 added to function.stream-set-timeout

From: Date: Sun, 20 Feb 2005 16:15:43 +0000
Subject: note 50172 added to function.stream-set-timeout
Groups: php.notes 
Request: Send a blank email to php-notes+get-85285@lists.php.net to get a copy of this message
[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. ?> ---- Manual Page -- http://www.php.net/manual/en/function.stream-set-timeout.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50172 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50172&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50172&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50172&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50172&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50172&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50172&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85285) next »