簡體   English   中英

Laravel-Guzzle:獲取GuzzleHttp \\ Exception \\ ConnectException:cURL錯誤6無法隨機解析主機

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM