Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

php - 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:

(有谁能帮我解决这个错误:)

  ask by doki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's a few things this could be;

(这可能有几件事;)

and generally its either a malformed URL or a networking issue outside the scope of cURL.

(通常是网址格式错误或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而不允许出站?)

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

(在这一点上,您的问题从卷曲转向网络和DNS,所以我不会更深入地进行调试……但这至少是我开始研究您的错误消息的地方。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...