stream_set_timeout

(PHP 4 >= 4.3.0, PHP 5)

stream_set_timeout -- Set timeout period on a stream

Description

bool stream_set_timeout ( resource stream, int seconds [, int microseconds] )

Sets the timeout value on stream, expressed in the sum of seconds and microseconds. 如果成功则返回 TRUE,失败则返回 FALSE

When the stream times out, the 'timed_out' key of the array returned by stream_get_meta_data() is set to TRUE, although no error/warning is generated.

例子 1. stream_set_timeout() example

<?php
$fp
= fsockopen("www.example.com", 80);
if (!
$fp) {
    echo
"Unable to open\n";
} else {

    
fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
    
stream_set_timeout($fp, 2);
    
$res = fread($fp, 2000);

    
$info = stream_get_meta_data($fp);
    
fclose($fp);

    if (
$info['timed_out']) {
        echo
'Connection timed out!';
    } else {
        echo
$res;
    }

}
?>

注: 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.

This function was previously called as set_socket_timeout() and later socket_set_timeout() but this usage is deprecated.

See also fsockopen() and fopen().


add a note add a note User Contributed Notes
alfi_ at yahoo dot com
01-Aug-2006 11:10
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
rtfm61 at yandex dot ru
25-Feb-2006 06:41
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.
ridera
21-Feb-2005 12:15
[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;        //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 = "
Finishedn";
$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.
?>
ridera
17-Feb-2005 09:37
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.