PHP crawls data through cURL (3): CURLINFO_HTTP_CODE returns 0 troubleshooting and solutions

1. The curl local server needs DNS to resolve the domain name

When using the curl command to send a request to the domain name address, the local server needs to perform DNS resolution to obtain the IP address corresponding to the domain name, otherwise, the curl command will not be able to establish a connection with the target server. When a request is sent using the curl command, curl will attempt to automatically resolve the provided URL to obtain an IP address. If the DNS resolution is successful, curl will use the obtained IP address to establish a connection. If DNS resolution fails, curl will not be able to connect to the target server.

Therefore, to ensure that the curl command works correctly on your local server, make sure your server can resolve DNS. You can check if your server is capable of DNS resolution by using the following command on a terminal:

ping example.com

where "example.com" is the domain name you need to test. If the command succeeds in DNS resolution, your local server can successfully resolve the domain name.

2. How to troubleshoot the cause of the error

Use curl_error($curlHandle) to troubleshoot the cause of the error

$host = "http://example.com/";
$curlHandle = curl_init($host);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_exec($curlHandle);
if (curl_errno($curlHandle)) {
    
    
    echo 'Error: ' . curl_error($curlHandle);
}
curl_close($curlHandle);

Error message:

Could not resolve: example.com (Could not contact DNS servers)

The "Could not resolve: example.comn (Timeout while contacting DNS servers)" error message indicates that the DNS of the server cannot be resolved, which may be caused by incorrect DNS configuration of the server or network connection problems. You can troubleshoot and solve by the following methods:

  1. Check DNS settings: Log in to your server to see if the DNS settings are correct. You can use the following command to check whether the DNS server address provided by the DNS service provider is correct:
  cat /etc/resolv.conf

If the DNS settings are correct, you can try updating the DNS cache and restarting the service to resolve the issue:

sudo systemctl restart network
sudo systemctl restart named
  1. Check network connection: Check whether the network connection of the server is normal. You can use the following command to test whether the connection is normal:

    ping www.lockdata.cn
    

    If there are network connectivity issues, you may need to contact your network administrator for resolution.

  2. Check firewalls and proxies: Check the firewall and proxy settings on your server to make sure DNS requests are not being blocked by a firewall or proxy. You can try to temporarily close the firewall or proxy to confirm whether it affects DNS queries.

  3. Use another DNS server: If you cannot verify the availability of your own DNS service provider, you can try to use the DNS server addresses provided by another DNS service provider.

3. The program upgrade solution that cannot resolve DNS

When using curl to send a request on a PHP server, if DNS cannot be resolved for some reason, the following methods can be used to avoid blocking:

  1. Set CURLOPT_TIMEOUT: Use CURLOPT_TIMEOUT to set the timeout period, if the curl request times out, the connection will be terminated automatically. Set CURLOPT_TIMEOUT with the following code:

    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    

    In this example, the timeout is set to 10 seconds. You can adjust this value to something that better suits your needs.

  2. Set CURLOPT_CONNECTTIMEOUT: Use CURLOPT_CONNECTTIMEOUT to set the connection timeout, if the curl connection times out, the connection will be terminated automatically. Set CURLOPT_CONNECTTIMEOUT with the following code:

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    

    In this example, the connection timeout is set to 5 seconds. Again, this value can be adjusted to something that better suits your needs.

  3. Set DNS resolution: If the problem of curl congestion caused by slow or failed DNS resolution still exists, you can try to manually set DNS resolution. You can use the following code to set up DNS resolution:

    curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 3600);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    

    In this example, the DNS resolution cache is set to 3600 seconds. Additionally, CURL_IPRESOLVE_V4 is used to specify the use of IPv4 addresses only.

4. How to configure DNS in Pagoda

1.etc/resolv.conf

insert image description here

2. Through the GUI interface

insert image description here
The above configuration method has the same effect.


@leak time sometimes

Guess you like

Origin blog.csdn.net/weixin_41290949/article/details/131144823