handle file_get_contents timeout including dns lookup time
| From: | nik600 | Date: | Tue, 14 Feb 2012 12:28:26 +0000 |
| Subject: | handle file_get_contents timeout including dns lookup time | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-316600@lists.php.net to get a copy of this message | ||
Dear all
i'm trying to handle a max timeout into a file_get_contents request,
this is my code:
******* client.php ******* placed on http://test.foo.com/client.php
<?php
ini_set('default_socket_timeout',10);
$ctx = stream_context_create(array('http' => array('timeout' => 10)));
$data = file_get_contents("http://www.foo.com/server.php",0,$ctx);
if(!data){
die("error during page request");
}else{
echo "OK";
echo $data;
}
?>
******* server.php ******* placed on www.foo.com at
http://www.foo.com/server.php
<?php
sleep(15);
echo "OK";
?>
i've noticed that in a standard scenario if i call
http://test.foo.com/client.php all works properly and
the timeout is
handled correctly.
But, if i'm experiencing dns lookup problems, the timeout setting is
ignored by file_get_contents, or more probably the socket isn't yet
estabilished so the counter for timeout doesn't start.
I've been able to reproduce this problem on a linux server dropping
incoming packets of DNS with this command: (be careful, it will
disable you ability to resolve all dns address!):
* iptables -A INPUT -p udp --sport 53 -j DROP
* iptables -A INPUT -p tcp --sport 53 -j DROP
So, the question is:
is possible to have a timeout check that includes also dns resolution time?
Thanks to all in advance