note 104597 added to function.curl-setopt
| From: | SteveKamerman at osu1 dot php dot net | Date: | Fri, 24 Jun 2011 18:47:48 +0000 |
| Subject: | note 104597 added to function.curl-setopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180930@lists.php.net to get a copy of this message | ||
If you want cURL to timeout in less than one second, you can use CURLOPT_TIMEOUT_MS, although there
is a bug/"feature" on "Unix-like systems" that causes libcurl to timeout
immediately if the value is < 1000 ms with the error "cURL Error (28): Timeout was
reached". The explanation for this behavior is:
"If libcurl is built to use the standard system name resolver, that portion of the transfer
will still use full-second resolution for timeouts with a minimum timeout allowed of one
second."
What this means to PHP developers is "You can use this function without testing it first,
because you can't tell if libcurl is using the standard system name resolver (but you can be
pretty sure it is)"
The problem is that on (Li|U)nix, when libcurl uses the standard name resolver, a SIGALRM is raised
during name resolution which libcurl thinks is the timeout alarm.
The solution is to disable signals using CURLOPT_NOSIGNAL. Here's an example script that
requests itself causing a 10-second delay so you can test timeouts:
<?php
if (!isset($_GET['foo'])) {
// Client
$ch = curl_init('http://localhost/test/test_timeout.php?foo=bar');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
echo "Data received: $data\n";
}
} else {
// Server
sleep(10);
echo "Done.";
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 99.9.85.218
----
Manual Page -- http://www.php.net/manual/en/function.curl-setopt.php
Edit -- https://master.php.net/note/edit/104597
Del: integrated -- https://master.php.net/note/delete/104597/integrated
Del: useless -- https://master.php.net/note/delete/104597/useless
Del: bad code -- https://master.php.net/note/delete/104597/bad+code
Del: spam -- https://master.php.net/note/delete/104597/spam
Del: non-english -- https://master.php.net/note/delete/104597/non-english
Del: in docs -- https://master.php.net/note/delete/104597/in+docs
Del: other reasons-- https://master.php.net/note/delete/104597
Reject -- https://master.php.net/note/reject/104597
Search -- https://master.php.net/manage/user-notes.php