![](/img/trans.png)
[英]Laravel - Guzzle Request / cURL error 6: Could not resolve host
[英]Laravel - Guzzle: Getting GuzzleHttp\Exception\ConnectException: cURL error 6 Could not resolve host randomly
这可能是我见过的最奇怪的情况。 基本上,我有一个内置于Laravel 5.1的系统,该系统需要向外部系统发出请求。 问题是,有时它可以工作,但有时我可以
GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: hosthere
就代码而言,绝对没有改变。 我使用完全相同的参数运行该应用程序,有时会收到错误,有时会得到正确的响应。
有任何想法吗?
提前致谢
编辑2当我执行nslookup domainthatineedtouse.com我得到
;; Got SERVFAIL reply from 8.8.8.8, trying next server
Server: 8.8.4.4
Address: 8.8.4.4#53
Non-authoritative answer:
Name: domainthatineedtouse.com
Address: therealipaddressishere
这可能与问题有关吗?
编辑这是建立连接的类的一部分。
<?php
use GuzzleHttp\ClientInterface;
class MyClass
{
const ENDPOINT = 'http://example.com/v1/endpoint';
/**
* @var \GuzzleHttp\ClientInterface
*/
protected $client;
/**
* @var string The api key used to connect to the service
*/
protected $apiKey;
/**
* Constructor.
*
* @param $apiKey
* @param ClientInterface $client
*/
public function __construct($apiKey, ClientInterface $client)
{
$this->client = $client;
$this->apiKey = $apiKey;
}
/**
* Here is where I make the request
*
* @param $param1
* @param $param2
* @return \Psr\Http\Message\ResponseInterface
*/
public function makeTheRequest($param1, $param2)
{
return $this->client->request('get', self::ENDPOINT, [
'query' => [
'api_token' => $this->apiKey,
'parameter_1' => $param1,
'parameter_2' => $param2,
]
]);
}
}
在调用外部API时,这通常是人们应该始终期望的事情,其中大多数都面临停机,包括非常流行的亚马逊产品广告API。 因此请记住,应始终将其放置在try / catch中,同时使用在do / while中放置重试。
您应该始终捕获这两种常见的超时类型,即连接超时和请求超时。 \\ GuzzleHttp \\ Exception \\ ConnectException和\\ GuzzleHttp \\ Exception \\ RequestException
// query External API
// retry by using a do/while
$retry_count = 0;
do {
try {
$response = json_decode($this->external_service->runOperation($operation));
} catch (\GuzzleHttp\Exception\ConnectException $e) {
// log the error here
Log::Warning('guzzle_connect_exception', [
'url' => $this->request->fullUrl(),
'message' => $e->getMessage()
]);
} catch (\GuzzleHttp\Exception\RequestException $e) {
Log::Warning('guzzle_connection_timeout', [
'url' => $this->request->fullUrl(),
'message' => $e->getMessage()
]);
}
// Do max 5 attempts
if (++$retry_count == 5) {
break;
}
} while(!is_array($response));
如果您与我们共享实际代码,可能会有所帮助。
我现在可以说的是,有可能是以下两个问题之一:
a)服务器上的DNS设置问题。 您需要尝试其他配置
b)您正在传递http://
作为网址的一部分。
我希望这有帮助。 基本上,问题与服务器有关:
https://twitter.com/digitalocean/status/844178536835551233
就我而言,我只需要重新启动即可,现在一切似乎都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.