|
stream_set_timeout
Set timeout period on a stream
(PHP 4 >= 4.3.0, PHP 5)
Sets the timeout value on stream,
expressed in the sum of seconds and
microseconds. Returns
When the stream times out, the 'timed_out' key of the array returned by
stream_get_meta_data() is set to Example 2383. stream_set_timeout() example<?php
Note:
As of PHP 4.3, this function can (potentially) work on any kind of stream. In PHP 4.3, socket based streams are still the only kind supported in the PHP core, although streams from other extensions may support this function.
Note:
This function doesn't work with advanced operations like stream_socket_recvfrom(), use stream_select() with timeout parameter instead. This function was previously called as set_socket_timeout() and later socket_set_timeout() but this usage is deprecated. See also fsockopen() and fopen(). Related Examples ( Source code ) » stream_set_timeout Examples ( Source code ) » Access a Microsoft WebService in PHP Code Examples / Notes » stream_set_timeoutridera
[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 \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. ?> rtfm61
stream_set_timeout() is not suitable for such files as UNIX-devices (/dev/...), i suggest to use select() instead with desirable timeout value - that works well.
alfi_
If you are using fsockopen() to create a connection, first going to write into the stream and then waiting for the reply (e.g. simulating HTTP request with some extra headers), then stream_set_timeout() must be set only after the write - if it is before write, it has no effect on the read timeout :-( Noticed at least on PHP/4.3.10 dianoga dianoga7
I have found that in order to actually stop the socket from timing out the script, you must call stream_get_meta_data and check for a timeout within the loop reading from the socket. Example: <?php $sock = fsockopen($host, 80, $errno, $errstr, 30); if(!$sock){ echo "Unable to get server status"; }else{ $out = "GET /server.php HTTP/1.1\r\n"; $out .= "Host: $host\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($sock, $out); stream_set_blocking($fp, FALSE ); stream_set_timeout($sock, $timeout); $info = stream_get_meta_data($sock); while (!feof($sock) && !$info['timed_out']) { $file .= fgets($sock, 4096); $info = stream_get_meta_data($sock); } fclose($sock); ?> ridera
I have found it required to add "stream_set_blocking($fp, FALSE )" prior to any fgets(), fread(), etc. to prevent the code from hanging up when remote files are called and the response is slow. martin butt - martin
Here is a working example for loops: <?php // Timeout in seconds $timeout = 5; $fp = fsockopen("www.server.com", 80, $errno, $errstr, $timeout); if ($fp) { fwrite($fp, "GET /file.php HTTP/1.0\r\n"); fwrite($fp, "Host: www.server.com\r\n"); fwrite($fp, "Connection: Close\r\n\r\n"); stream_set_blocking($fp, TRUE); stream_set_timeout($fp,$timeout); $info = stream_get_meta_data($fp); while ((!feof($fp)) && (!$info['timed_out'])) { $data .= fgets($fp, 4096); $info = stream_get_meta_data($fp); ob_flush; flush(); } if ($info['timed_out']) { echo "Connection Timed Out!"; } else { echo $data; } } ?> |
Change Languagestream_bucket_append stream_bucket_make_writeable stream_bucket_new stream_bucket_prepend stream_context_create stream_context_get_default stream_context_get_options stream_context_set_option stream_context_set_params stream_copy_to_stream stream_encoding stream_filter_append stream_filter_prepend stream_filter_register stream_filter_remove stream_get_contents stream_get_filters stream_get_line stream_get_meta_data stream_get_transports stream_get_wrappers stream_register_wrapper stream_resolve_include_path stream_select stream_set_blocking stream_set_timeout stream_set_write_buffer stream_socket_accept stream_socket_client stream_socket_enable_crypto stream_socket_get_name stream_socket_pair stream_socket_recvfrom stream_socket_sendto stream_socket_server stream_socket_shutdown stream_wrapper_register stream_wrapper_restore stream_wrapper_unregister |