如果请求期间发生错误,Guzzle 会引发异常。不幸的是,似乎没有特定于超时的错误 - 这对我来说很重要,因为我知道这些错误偶尔会发生。我想重试相应的请求,并且需要能够判断错误是否是由于超时而发生的。
来自docs http://docs.guzzlephp.org/en/latest/clients.html#decode-content:
// 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'
The RequestException
其信息中有message
财产:
"cURL error 28: Operation timed out after 3114 milliseconds with 0 bytes received"
所以我可以评估消息模式,但这感觉有点不对,因为这些消息将来很容易改变。
使用 guzzle 4 时是否有更好/更稳定的方法来检查超时?
我遇到了同样的问题,我已经通过停止事件的传播来解决它。您可以阅读更多相关内容here http://guzzle.readthedocs.org/en/latest/events.html#event-propagation.
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Message\Response;
$client->getEmitter()->on('error', function(ErrorEvent $event) {
$event->stopPropagation();
$event->intercept(new Response(200));
echo $event->getException()->getMessage();
});
在你的情况下,这将输出cURL error 28: Operation timed out after 3114 milliseconds with 0 bytes received
不扔RequestException
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)