php cURL 请求错误:名称查找超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13153557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:51:35  来源:igfitidea点击:

Error in cURL request: name lookup timed out

phpzend-frameworkcurldnszend-http-client

提问by jabbar

I wrote some code that fill a login form and submit it via post method. Like:

我写了一些代码来填写登录表单并通过 post 方法提交它。喜欢:

    $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?

有什么问题?我能做些什么来解决它?

回答by herderwu

I encountered the same problem:

我遇到了同样的问题:

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

After restarting Apache, it worked. So strange.

重新启动Apache后,它工作了。这么奇怪。

回答by luigif

It can be a timeoutproblem. Try adjusting the connection timeout:

这可能是超时问题。尝试调整连接超时:

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

you can also set single curloptions:

您还可以设置单卷曲选项:

$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 forloop with a number of retries.

如果您发现这是超时问题,我不建议将超时参数增加太多,而是建议进行多次重试的for循环。

回答by sagi

It means that your DNS server failed to return a response in time. Check your DNS configuration (e.g. /etc/resolv.conf on Linux), and make sure they are alive and functional. 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).

这意味着您的 DNS 服务器未能及时返回响应。检查您的 DNS 配置(例如 Linux 上的 /etc/resolv.conf),并确保它们处于活动状态且功能正常。还尝试从同一服务器 ping URL 中的主机,以了解问题是仅在 PHP 中还是在服务器上运行的任何应用程序中(更有可能)。