简体   繁体   English

cURL 错误 #:无法解析主机:- PHP

[英]cURL Error #:Could not resolve host: - PHP

I have problem with this code:我对这段代码有问题:

function get_request($url,$header_array){

        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => $url,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_HTTPHEADER => $header_array,
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
          echo "cURL Error #:" . $err;
        } else {
          return $response;
        }

    }

I've got this error:我有这个错误:

cURL Error #:Could not resolve host:

I know there are a lot of questions like this, but the answer from those questions didn't help me, because its different code.我知道有很多这样的问题,但这些问题的答案对我没有帮助,因为它的代码不同。 Does anyone can help me how to fix this error:有没有人可以帮我解决这个错误:

There's a few things this could be;这可能是一些事情; and generally its either a malformed URL or a networking issue outside the scope of cURL.通常是格式错误的 URL 或 cURL 范围之外的网络问题。

First, save yourself some time and just double and triple check you've typed the URL correctly.首先,为自己节省一些时间,并仔细检查您是否正确输入了 URL。 I do this time and time again =D我一次又一次地这样做=D

Next up, on the same machine and under the same user as your PHP code executes, run something like curl -sS http://yoururl.com/example from the command line.接下来,在执行 PHP 代码的同一台机器上,在同一用户下,从命令行运行curl -sS http://yoururl.com/example类的东西。 This will help you double check your code can actually make outbound HTTP requests.这将帮助您仔细检查您的代码是否可以实际发出出站 HTTP 请求。

If your curl command fails in bash, then you can look at a few causes depending on the errors:如果您的 curl 命令在 bash 中失败,那么您可以根据错误查看一些原因:

  • a networking issue (has your local/dev machine lost internet connectivity?)网络问题(您的本地/开发机器是否丢失了互联网连接?)
  • a firewall issue;防火墙问题; is your firewall configured to allow inbound HTTP(s) and not outbound?您的防火墙是否配置为允许入站 HTTP(s)而不是出站?
  • is it a DNS issue (my favourite!).是 DNS 问题吗(我最喜欢的!)。 If the domain is new, perhaps it hasn't proposed to your server's upstream DNS yet.如果域是新的,可能它还没有向您服务器的上游 DNS 提出建议。

At this point, your question turns from curl to networking and DNS so I don't dive deeper into debugging... but this is, at least, where I'd start in looking into your error message.在这一点上,您的问题从 curl 转向了网络和 DNS,因此我不会深入研究调试……但这至少是我开始查看您的错误消息的地方。

reading the symptoms, it is possible the DNS resolver that you use is probably too busy (timeout) -or- limit the number of inquiry per IP address in a certain timespan.阅读症状,您使用的 DNS 解析器可能太忙(超时) - 或 - 限制特定时间跨度内每个 IP 地址的查询次数。 So, the same exact process will probably give a different error if run on a different day.因此,如果在不同的日期运行,相同的确切过程可能会产生不同的错误。

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

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