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));
?>
stream_set_timeout
(PHP 4 >= 4.3.0, PHP 5)
stream_set_timeout — Akımın zaman aşımı süresini ayarlar
Açıklama
$akım
, int $saniye
[, int $mikrosaniye = 0
] )
Belirtilen akım'ın zaman aşımı süresini
saniye ve mikrosaniye
toplamına ayarlar.
Akım zaman aşımına uğrarsa stream_get_meta_data() işlevi
tarafından döndürülen dizinin 'timed_out' anahtarına
TRUE atanır, fakat herhangi bir hata veya uyarı üretilmez.
Değiştirgeler
-
akım -
Hedef akım.
-
saniye -
Zaman aşımı süresinin saniye sayısı.
-
mikrosaniye -
Zaman aşımı süresinin son saniyesinden sonraki mikrosaniyelerin sayısı.
Dönen Değerler
Başarı durumunda TRUE, başarısızlık durumunda FALSE döner.
Sürüm Bilgisi
| Sürüm: | Açıklama |
|---|---|
| 4.3.0 | PHP 4.3'ten itibaren bu işlev (hemen hemen) her çeşit akımla çalışabilmektedir. PHP 4.3'te soketlere dayalı akımlar, PHP çekirdeği tarafından desteklenen tek akım türü olmakla beraber diğer eklentilerdeki akımlar bu işlevi destekleyebiliyordu. |
Örnekler
Örnek 1 - stream_set_timeout() örneği
<?php
$fp = fsockopen("www.example.com", 80);
if (!$fp) {
echo "Dosya açılamadı\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 'Bağlantı zaman aşımına uğradı!';
} else {
echo $res;
}
}
?>
Notlar
Bilginize:
Bu işlev, stream_socket_recvfrom() gibi ileri düzey işlemler için çalışmaz. Onlar için zaman aşımını stream_select() işlevi ile ayarlayın.
Bu işlevin ismi başta set_socket_timeout() idi, sonra socket_set_timeout() oldu, fakat artık bu isimlerin kullanımı önerilmiyor.
Ayrıca Bakınız
- fsockopen() - Bir internet veya Unix alan soketi bağlantısı açar
- fopen() - Bir dosya veya URL'yi açar
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.
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;
}
}
?>
This function seems to have no effect when running as a CLI script, see http://bugs.php.net/bug.php?id=36030
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);
?>
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
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.
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.
[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.
?>
