Why does file_get_contents take twice the time of default_socket_tieout when timeout occurs?
| From: | masahiro_honma | Date: | Tue, 31 Mar 2015 04:09:40 +0000 |
| Subject: | Why does file_get_contents take twice the time of default_socket_tieout when timeout occurs? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-324743@lists.php.net to get a copy of this message | ||
Hello,
I use file_get_contents to access to a web api.
It's timeout is set by default_socket_timeout.
When a web api took a long time to response, of course file_get_contents timed out.
But It seemed strange. file_get_contents took more time than default_socket_timeout.
For reproducing this problem,
I prepared the program that took enough time to cause timeout and I tested file_get_contents.
As a result, I found that file_get_contents takes twice the time of default_socket_tieout when
timeout occurs.
For example, when I set default_socket_timeout to 3 sec, file_get_contents takes 6 seconds to return
the result.
Why does file_get_contents take twice the time of default_socket_tieout when timeout occurs?
######
# code #
######
--------------------------------------------------
<?php
ini_set('default_socket_timeout', 3);
echo file_get_contents('http://foo.bar.jp/sleep');
--------------------------------------------------
# If I use curl module, this problem doesn't occur.
--------------------------------------------------
<?php
$ch = curl_init('http://foo.bar.jp/sleep');
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_exec($ch);
curl_close($ch);
--------------------------------------------------
##########
# php version #
##########
$ php --version
PHP 5.4.6 (cli) (built: Jun 19 2014 17:43:25)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
------
Masahiro Honma