发布时间:2025-12-09 11:44:30 浏览次数:1
curl 的功能非常强大, 参数也很繁多, 我们不仅常用于命令行, 在php中也有类似 curl 拓展的实现, 并且也对 libcurl 库提供了非常好的支持.
curl 项目: https://github.com/curl/curl
curl --help--connect-timeout SECONDS Maximum time allowed for connection-m, --max-time SECONDS Maximum time allowed for the transfer...--retry NUM Retry request NUM times if transient problems occur--retry-delay SECONDS Wait SECONDS between retries--retry-max-time SECONDS Retry only within this period上面是我整理的一部分跟时间有关系的参数, 虾米啊我们依次实验下.
说明
--connect-timeout SECONDS Maximum time allowed for connection示例
#这里我们设置超时时间为2s, 请求一个无法解析的地址curl --connect-timeout 2 --url http://xxx.comcurl: (28) Connection timed out after 2002 milliseconds“显示连接超时, 超时时间2002毫秒. 注意这个 warning 的时间可能每次统计不太一样, 一般会超过我们的预设值一点.
#对于一个对返回时间要求比较高的情况, 可以设置为浮点型精确到毫秒curl --connect-timeout 0.3 --url http://xxx.comcurl: (28) Connection timed out after 300 milliseconds说明
-m, --max-time SECONDS Maximum time allowed for the transfer示例
#这里我们设置超时时间为2s, 应用程序中sleep 2curl --max-time 2 --url http://www.shuai.comcurl: (28) Operation timed out after 2002 milliseconds with 0 bytes receivedconnect-time 和 max-time 联合使用:
#这里我们使用了一个无法解析的地址curl --connect-time 3 --max-time 2 --url http://xxx.com> curl: (28) Connection timed out after 2001 millisecondscurl --connect-time 3 --max-time 4 --url http://xxx.com> curl: (28) Operation timed out after 4002 milliseconds with 0 bytes received“这里我们发现返回结果为连接超时 2001 毫秒, 当共同使用时, 连接以最小时间的为准, 而返回时间已 max-time 限制为准.
说明
--retry NUM Retry request NUM times if transient problems occur示例
#同样,我们去请求一个 sleep 2 的地址curl --max-time 0.1 --retry 3 --url http://www.shuai.com> Warning: Transient problem: timeout Will retry in 1 seconds. 3 retries left.> Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.> Warning: Transient problem: timeout Will retry in 4 seconds. 1 retries left.> curl: (28) Operation timed out after 100 milliseconds with 0 bytes received“我们发现重试了3次, 但它并不是失败后立刻重试, 而是第一次 1 s后重试, 第二次 2 s后重试, 第三次 4 s后重试,依次递增 (每次重试受 max-time 限制).
我们发现我们的 max-time 只是对单次请求做了时间限制, 进而去影响总的重试时间, 但是我们想在单位时间内完成重试该怎么做呢. 这里 curl 也提供了重试的超时时间 retry-max-time
curl --retry 3 --retry-max-time 2 --max-time 0.1 --url http://www.shuai.com> Warning: Transient problem: timeout Will retry in 1 seconds. 3 retries left.> Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.> curl: (28) Operation timed out after 101 milliseconds with 0 bytes received“我们对重试总的超时时间设置为2s, 配置了3次重试, 但仅仅完成了两次重试就超时结束了.
我们在 请求重试 里面讲到, 这里的重试并不是失败后立刻重试的, 默认重试时间递增, 这里我们可以使用 retry-delay 控制重试的间隔.
#这里我们设置重试时间5s,重试3次curl --retry 3 --retry-delay 5 --max-time 0.1 --url http://xxx.com> Warning: Transient problem: timeout Will retry in 5 seconds. 3 retries left.> Warning: Transient problem: timeout Will retry in 5 seconds. 2 retries left.> Warning: Transient problem: timeout Will retry in 5 seconds. 1 retries left.> curl: (28) Connection timed out after 101 milliseconds“我们发现 Will retry in 变成了 5 s一次
Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上.
快速安装
{ "require": { "guzzlehttp/guzzle": "~5.3|~6.0" }, "repositories": { "packagist": { "type": "composer", "url": "https://mirrors.aliyun.com/composer/" } }}执行 composer
composer install示例代码(超时机制)
require "./vendor/autoload.php";$client = new GuzzleHttpClient();$res = $client->request('GET', 'http://xxx.com', [ 'connect_timeout' => 3, 'timeout' => 2, ]);echo $res->getBody() . PHP_EOL;output:
PHP Fatal error: Uncaught GuzzleHttpExceptionConnectException:cURL error 28: Connection timed out after 2002 milliseconds(see https://curl.haxx.se/libcurl/c/libcurl-errors.html)....“我们配置了 connect_timeout 超时时间 3 s, timeout超时时间 2 s
重试机制比较麻烦一点, 需要使用 Middleware 来实现, 但也很好理解
require "./vendor/autoload.php";$handlerStack = GuzzleHttpHandlerStack::create();//绑定中间件, 重试三次$restryCount = 3;$handlerStack->push(GuzzleHttpMiddleware::retry(function () use (&$restryCount) { if (--$restryCount <= 0) { return false; } return true;}, function () use ($restryCount) { return 1000; //毫秒}));$client = new GuzzleHttpClient(['handler' => $handlerStack]);$res = $client->request('GET', 'http://xxx.xxx.com', [ 'connect_timeout' => 3, 'timeout' => 2, ]);echo $res->getBody() . PHP_EOL;“在定义 retry 的时间, 你需要去实现是否继续重试, 重试的时间等策略, 提供了巨大的重试灵活性.
“值得注意的是 curl 的重试时间单位是秒, 而这里是设置的毫秒.