douyi5961
douyi5961
2017-03-21 16:58
浏览 491
已采纳

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

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. 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

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

图片转代码服务由CSDN问答提供 功能建议

这可能是我见过的最奇怪的情况。 基本上我有一个内置在Laravel 5.1中的系统,它需要向外部系统发出请求。 问题是,有时它可以工作,但有时我得到

  GuzzleHttp \ Exception \ ConnectException:cURL错误6:无法解析主机:hosthere 
   
 
 

就代码而言,绝对没有任何变化。 我使用完全相同的参数运行应用程序,有时我得到错误,有时我得到正确的响应。

任何想法?

谢谢 提前

编辑2 当我执行nslookup domainthatineedtouse.com时,我得到

  ;; 从8.8.8.8获得SERVFAIL回复,尝试下一个服务器
服务器:8.8.4.4 
地址:8.8.4.4#53 
 
非权威答案:
名称:domainthatineedtouse.com 
地址:therealipaddressishere 
  <  / pre> 
 
 

这可能与此问题有关吗?

编辑 这是进行连接的类的一部分。

 &lt;?php 
 
 
use GuzzleHttp \ ClientInterface; 
 
class MyClass 
 {
 const ENDPOINT ='http://example.com/  v1 / endpoint'; 
 
 / ** 
 * @var \ GuzzleHttp \ ClientInterface 
 * / 
 protected $ client; 
 
 / ** 
 * @var string用于连接的api密钥 到服务
 * / 
 protected $ apiKey; 
 
 / ** 
 *构造函数。
 * 
 * @param $ apiKey 
 * @param ClientInterface $ client 
 * / 
 public  function __construct($ apiKey,ClientInterface $ client)
 {
 $ this-&gt; client = $ client; 
 $ this-&gt; apiKey = $ apiKey; 
} 
 
 / ** 
 * 这是我发出请求的地方
 * 
 *  @param $ param1 
 * @param $ param2 
 * @return \ Psr \ Http \ Message \ ResponseInterface 
 * / 
公共函数makeTheRequest($ param1,$ param2)
 {
返回$ this-&gt  ; client-&gt; request('get',self :: ENDPOINT,[
'query'=&gt;  [
'api_token'=&gt;  $ this-&gt; apiKey,
'parameter_1'=&gt;  $ param1,
'parameter_2'=&gt;  $ param2,
] 
]); 
} 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dqbh8054
    dqbh8054 2017-03-22 15:02
    已采纳

    I hope this helps. Basically the problem was related to the server:

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

    In my case, I just needed to reboot and everything seems fine now.

    点赞 评论
  • douqiaotong8682
    douqiaotong8682 2017-03-21 17:14

    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. You need to try different configuration

    b) you are passing http:// as part of the url.

    点赞 评论
  • dragon_9000
    dragon_9000 2017-03-22 01:38

    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. So keeping that in mind this should always be placed in a try/catch along with using retries placed in a do/while.

    You should always catch these two types of common timeouts, connection timeouts and request timeouts. \GuzzleHttp\Exception\ConnectException and \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));
    
    点赞 评论

相关推荐