简体   繁体   English

cURL请求出错:名称查找超时

[英]Error in cURL request: name lookup timed out

I wrote some code that fill a login form and submit it via post method. 我写了一些填写登录表单的代码,并通过post方法提交。 Like: 喜欢:

    $config = array(
        'adapter' => 'Zend_Http_Client_Adapter_Curl',
    );      

    $this->siteObj = new Zend_Http_Client('http://example.com', $config);
    $this->siteObj->setCookieJar();
    $this->siteObj->setUri('http://example.com/login');
    $this->siteObj->setParameterPost( 'data[User][name]', 'user' );
    $this->siteObj->setParameterPost( 'data[User][password]', 'password' );
    $response = $this->siteObj->request('POST');

its works fine, but some times this error occur: 它的工作正常,但有时会发生此错误:

Error in cURL request: name lookup timed out

Error: An Internal Error Has Occurred.

whats the problem? 有什么问题? what can I do to solve it? 我能做些什么来解决它?

I encountered the same problem: 我遇到了同样的问题:

  • From the shell, curl worked. 从外壳,卷曲工作。
  • From the shell, the PHP script worked. 从shell开始,PHP脚本起作用了。
  • PHP could not ping the website. PHP无法ping通网站。
  • The DNS config was right. DNS配置是对的。

After restarting Apache, it worked. 重新启动Apache后,它工作正常。 So strange. 这么奇怪。

It can be a timeout problem. 这可能是一个超时问题。 Try adjusting the connection timeout: 尝试调整连接超时:

$config = array(
  'adapter' => 'Zend_Http_Client_Adapter_Curl',
  'timeout' => 100
);

you can also set single curl options: 您还可以设置单个卷曲选项:

$config = array(
    'adapter'   => 'Zend_Http_Client_Adapter_Curl',
    'curloptions' => array(
        CURLOPT_USERAGENT      => 'Zend_Curl_Adapter',
        CURLOPT_HEADER         => 0,
        CURLOPT_VERBOSE        => 0,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_TIMEOUT        => 10,
        CURLOPT_SSL_VERIFYPEER => false,
    ),
);

If you find out that it is a timeout issue, I would not suggest to increase too much the timeout parameter, but rather to make a for loop with a number of retries. 如果你发现它是一个超时问题,我不建议增加过多的timeout参数,而是建立一个带有多次重试的for循环。

It means that your DNS server failed to return a response in time. 这意味着您的DNS服务器无法及时返回响应。 Check your DNS configuration (eg /etc/resolv.conf on Linux), and make sure they are alive and functional. 检查您的DNS配置(例如Linux上的/etc/resolv.conf),并确保它们存活且功能正常。 Also try to ping the host in the URL from the same sever to get an idea whether the problem only in PHP or the any application running on the server (more likely). 还尝试在同一服务器的URL中ping主机,以了解问题是仅在PHP中还是在服务器上运行的任何应用程序(更有可能)。

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

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