downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

stream_set_write_buffer> <stream_set_blocking
Last updated: Fri, 13 Nov 2009

view this page in

stream_set_timeout

(PHP 4 >= 4.3.0, PHP 5)

stream_set_timeoutストリームにタイムアウトを設定する

説明

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

stream にタイムアウトの値を設定します。 この値は、secondsmicroseconds の和で表されます。

ストリームがタイムアウトとなった場合は、 stream_get_meta_data() が返す配列のキー 'timed_out' の値が TRUE に設定されます。エラーや警告が発生していなくても同様になります。

パラメータ

stream

対象となるストリーム。

seconds

設定したいタイムアウトの秒数部分。

microseconds

設定したいタイムアウトのマイクロ秒数部分。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

変更履歴

バージョン 説明
4.3.0 PHP 4.3 より、この関数は、(潜在的には)どの種類の ストリームに対しても機能するようになりました。 PHP 4.3 では、ソケットベースのストリームが、 唯一この関数でサポートされている種類でしたが、他の拡張モジュール由来の モジュールはこの機能をサポートしているかもしれません。

例1 stream_set_timeout() の例

<?php
$fp 
fsockopen("www.example.com"80);
if (!
$fp) {
    echo 
"開けません\n";
} else {

    
fwrite($fp"GET / HTTP/1.0\r\n\r\n");
    
stream_set_timeout($fp2);
    
$res fread($fp2000);

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

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

}
?>

注意

注意: この関数では、stream_socket_recvfrom() のような 高度な操作はできません。そのかわりに、timeout パラメータを指定して stream_select() を使用してください。

この関数は、以前は set_socket_timeout() 、その後は socket_set_timeout() と呼ばれたこともありましたが、 これらの利用は推奨されません。

参考

  • fsockopen() - インターネット接続もしくはUnix ドメインソケット接続をオープンする
  • fopen() - ファイルまたは URL をオープンする


stream_set_write_buffer> <stream_set_blocking
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
stream_set_timeout
amckee3 at gmail dot com
02-Sep-2009 11:08
I about killed myself over this:

As of now, (php 5.3) stream_set_timeout does not work on any ssl (secure/encrypted) connection, its simply ignored. If data is not returned when you try to read the socket it will keep on going until the max execution time.
Martin Butt - martin at anti_spambutt.cx
13-Mar-2007 02:39
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;
        }
}
?>
Dianoga (dianoga7 [at] 3dgo.net)
20-Nov-2006 11:33
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);
?>
alfi_ at yahoo dot com
02-Aug-2006 12: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 07: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 01: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 = "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.
?>
ridera
17-Feb-2005 10: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.

stream_set_write_buffer> <stream_set_blocking
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites