简体   繁体   English

Laravel-Guzzle:获取GuzzleHttp \\ Exception \\ ConnectException:cURL错误6无法随机解析主机

[英]Laravel - Guzzle: Getting GuzzleHttp\Exception\ConnectException: cURL error 6 Could not resolve host randomly

This is probably the weirdest situation that I ever seen. 这可能是我见过的最奇怪的情况。 Basically I have a system built in Laravel 5.1 which needs to make a request to an external system. 基本上,我有一个内置于Laravel 5.1的系统,该系统需要向外部系统发出请求。 The thing is, sometimes it works but sometimes I get 问题是,有时它可以工作,但有时我可以

GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: hosthere

Absolutely nothing changes, in terms of code. 就代码而言,绝对没有改变。 I run the application with exactly the same parameters and sometimes I get the error, sometimes I get the right response. 我使用完全相同的参数运行该应用程序,有时会收到错误,有时会得到正确的响应。

Any ideas? 有任何想法吗?

Thanks in advance 提前致谢

EDIT 2 When I execute nslookup domainthatineedtouse.com I get 编辑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

Can this be related to the issue? 这可能与问题有关吗?

EDIT This is part of the class that is making the connection. 编辑这是建立连接的类的一部分。

<?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,
            ]
        ]);
    }
}

This is something one should always anticipate while calling external APIs most of them face outages including the very popular Amazon's Product Advertising API. 在调用外部API时,这通常是人们应该始终期望的事情,其中​​大多数都面临停机,包括非常流行的亚马逊产品广告API。 So keeping that in mind this should always be placed in a try/catch along with using retries placed in a do/while. 因此请记住,应始终将其放置在try / catch中,同时使用在do / while中放置重试。

You should always catch these two types of common timeouts, connection timeouts and request timeouts. 您应该始终捕获这两种常见的超时类型,即连接超时和请求超时。 \\GuzzleHttp\\Exception\\ConnectException and \\GuzzleHttp\\Exception\\RequestException \\ 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));

It could be helpful if you share actual code with us. 如果您与我们共享实际代码,可能会有所帮助。


What I can say now, there are chances to be one of 2 issues: 我现在可以说的是,有可能是以下两个问题之一:

a) DNS settings problem on your server. a)服务器上的DNS设置问题。 You need to try different configuration 您需要尝试其他配置

b) you are passing http:// as part of the url. b)您正在传递http://作为网址的一部分。

I hope this helps. 我希望这有帮助。 Basically the problem was related to the server: 基本上,问题与服务器有关:

https://twitter.com/digitalocean/status/844178536835551233 https://twitter.com/digitalocean/status/844178536835551233

In my case, I just needed to reboot and everything seems fine now. 就我而言,我只需要重新启动即可,现在一切似乎都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM