ConFoo: Call for paper is now Open

stream_set_timeout

(PHP 4 >= 4.3.0, PHP 5)

stream_set_timeoutConfigure la durée d'expiration d'un flux

Description

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

stream_set_timeout() configure la durée d'expiration du flux stream, exprimé comme la durée de seconds secondes et microseconds microsecondes.

Lorsque le flux se termine, la clé 'timed_out' du tableau retourné par stream_get_meta_data() est définie à TRUE, cependant, aucune erreur ou alerte n'est générée.

Liste de paramètres

stream

Le flux cible.

seconds

Le nombre de secondes entières du délai d'expiration.

microseconds

Le nombre de microsecondes entières du délai d'expiration.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.

Historique

Version Description
4.3.0 Depuis PHP 4.3, cette fonction peut (potentiellement) fonctionner avec n'importe quel flux. Avant PHP 4.3, les flux utilisant des sockets sont les seuls qui soient supportés dans le coeur de PHP, même si les autres extensions pourraient supporter cette fonction.

Exemples

Exemple #1 Exemple avec stream_set_timeout()

<?php
$fp 
fsockopen("www.example.com"80);
if (!
$fp) {
     echo 
"Impossible d'ouvrir\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 
'Délai de connexion dépassé !';
  } else {
     echo 
$res;
  }

}
?>

Notes

Note:

Cette fonction ne fonctionne pas avec les opérations avancées comme stream_socket_recvfrom(), utilisez plutôt stream_select() avec une durée d'expiration en paramètre.

Cette fonction était appelée auparavant set_socket_timeout(), et aussi socket_set_timeout(), mais ces appellations sont obsolètes.

Voir aussi

  • fsockopen() - Ouvre un socket de connexion Internet ou Unix
  • fopen() - Ouvre un fichier ou une URL
add a note add a note

User Contributed Notes 9 notes

up
11
hamishcool3 at yahoo dot co dot uk
3 years ago
In case anyone is puzzled, stream_set_timeout DOES NOT work for sockets created with socket_create or socket_accept. Use socket_set_option instead.

Instead of:
<?php
stream_set_timeout
($socket,$sec,$usec);
?>

Use:
<?php
socket_set_option
($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>$sec, 'usec'=>$usec));
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>$sec, 'usec'=>$usec));
?>
up
2
Martin Butt - martin at anti_spambutt.cx
7 years ago
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;
        }
}
?>
up
0
emailfire at gmail dot com
3 years ago
This function seems to have no effect when running as a CLI script, see http://bugs.php.net/bug.php?id=36030
up
0
alfi_ at yahoo dot com
8 years ago
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
up
0
rtfm61 at yandex dot ru
8 years ago
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.
up
0
ridera
9 years ago
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.
up
-1
jack.whoami
3 months ago
Just in case someone stumbles into my situation... I was trying to get the microseconds part to work and it just wasn't working as expected.

Assuming blocking mode is true and I use

<?php stream_set_timeout($s, 1, 0);?>

I get a 1s delay as expected. However when I do

<?php stream_set_timeout($s, 0, 500);?>

I expect a 500microsecond delay but I get no delays at all. However when i do this

<?php stream_set_timeout($s, 0, 500000);?>

I get a 500 microsecond delay which is what I was expecting
up
-1
Dianoga (dianoga7 [at] 3dgo.net)
7 years ago
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);
?>
up
-1
mildly dull at terriblyclever dot com
4 years ago
I didn't have much luck with the suggestions below (although I likely applied them wrong).
Instead, I used stream_context_create() and set an http option for timeout. I fed that context into file_get_contents() and voila!

To my desperate friend below: the https transport can also use the http stream context options. I haven't verified this works as I don't have a slow responding ssl to test on. But if you are still stressing, give the below a shot (you may need to modify a bit...)

<?php
    $timeout
= 5*60;
   
$options = array(
         
'http'=>array(
           
'method'=>"GET",
           
'header'=>"Accept-language: en\r\n",
             
'timeout' => $timeout
             
)
        );
   
$context = stream_context_create($options);
   
$contents = file_get_contents($source, false, $context);
?>

Yes...that is a 5 minute timeout.
To Top