Hir*_*ter 11 php timeout exception guzzle
如果在请求期间发生错误,Guzzle会抛出异常.不幸的是,似乎没有特定于超时的错误 - 这对我来说很重要,因为我知道那些可能会偶尔发生.我想重试相应的请求,并且需要能够判断是否由于超时而发生错误.
来自文档:
// Timeout if a server does not return a response in 3.14 seconds.
$client->get('/delay/5', ['timeout' => 3.14]);
// PHP Fatal error: Uncaught exception 'GuzzleHttp\Exception\RequestException'
Run Code Online (Sandbox Code Playgroud)
该RequestException在其信息message属性:
"cURL error 28: Operation timed out after 3114 milliseconds with 0 bytes received"
Run Code Online (Sandbox Code Playgroud)
所以我可以评估消息模式,但这感觉有点不对,因为这些消息将来很容易改变.
使用guzzle 4时是否有更好/更稳定的方法来检查超时?
小智 -1
异常是在这里生成的:
https://github.com/guzzle/guzzle/blob/master/src/Adapter/Curl/CurlAdapter.php
private function handleError(
TransactionInterface $transaction,
$info,
$handle
) {
$error = curl_error($handle);
$this->releaseEasyHandle($handle);
RequestEvents::emitError(
$transaction,
new AdapterException("cURL error {$info['curl_result']}: {$error}"),
$info
);
}
Run Code Online (Sandbox Code Playgroud)
虽然这是一个私有函数,但您有两个选择: