CURL reported Could not resolve host error in php

1. Use the file_get_contents () function. Empty is returned if the parameter is the domain name address, and data is available when the ip address is used.
2. You can ping the domain name
3 and php-cli in linux, and you can access the remote domain name and get the data.
4. Remote domain names cannot be accessed through browsers, that is, php-fpm mode curl and file_get_contents.
5. After adding 220.181.112.244 www.baidu.com to / etc/hosts, you can access the data in php-fpm mode.

Code:

echo 1;
try {
    var_dump(file_get_contents("http://220.181.112.244/index.html"));
//    var_dump(file_get_contents("http://www.baidu.com"));
} catch (Exception $e) {
    print_r($e->getTrace());
}
echo 2;

1. Parameter: http://www.baidu.com

clipboard.png

2:http://220.181.112.244/index....

clipboard.png

3

clipboard.png

this is the error message requested by the file_get_contents () method:

2018/05/26 22:16:03 [error] 24942-sharp0: *46192 FastCGI sent in stderr: "PHP message: PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /data/com.9b/duolaixue_admin/test_print/test.php on line 13

error message for php curl request:
Could not resolve host: www.baidu.com; Name or service not known

Mar.13,2021

this problem can be solved occasionally, but not occasionally. Generally, if it is centos7, it can be solved by restarting it with systemctl restart php-fpm;
if it is centos6, use service php-fpm restart


the same problem, the same details. Personal test, restart php-fpm can be broken.
(I find that most of the baffling curl problems are broken by restarting php-fpm.)


if it is a domain name, you need to configure DNS on the server side. See if the DNS on your server can resolve the ip corresponding to the domain name.


1. If you put your above code to run on the server, Baidu's server may recognize you as USERAGENT for robot robot, to deny your access

2, file_get_contents () is feasible in some cases, but in some special cases, you can also use the function of the curl library. In order to prevent the other party from recognizing that the browser user you can access the server under the guise of robot, is Mozilla/4.0, for example, the following code

<?php 
$curl = curl_init(); 
//USERAGENT
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 

$xxx = curl_exec($curl); 

curl_close ($curl); 
echo $xxx; 
?>
For

curl-related parameters, please see http://php.net/manual/en/func.

.

add:
about your question, I ran this code successfully

<?php
 $url = "http://www.baidu.com"; 
        $page = "/services/calculation"; 
        $headers = array( 
            "POST ".$page." HTTP/1.0", 
            "Content-type: text/xml;charset=\"utf-8\"", 
            "Accept: text/xml", 
            "Cache-Control: no-cache", 
            "Pragma: no-cache", 
            "SOAPAction: \"run\"", 
        ); 
       
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL,$url); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 

        $data = curl_exec($ch); 

        if (curl_errno($ch)) { 
            print "Error: " . curl_error($ch); 
        } else { 
            // Show me the result 
            var_dump($data); 
            curl_close($ch); 
        } 
?>

clipboard.png


has the main problem been solved? I have the same problem today. Ask for help


the problem that has been bothering me for a long time suddenly occurred to me that it would be nice to reinstall PHP,.

turn off the php-fpm process, then recompile and install the same version of php to return to normal.

Menu