Cause:
If IPv6 is enabled, curl will resolve IPv6 first by default. If the corresponding domain name does not have IPv6, curl will wait until the IPv6 dns resolution fails timeout before looking for IPv4 in the previous normal process. In the program, I set a strict timeout limit on the content obtained by curl, which may cause the problem that the content cannot be obtained.
The solution is to set the default access to ipv4. The curl setting method of php is as follows:
The code is as follows: |
Copy code |
$ Ch = curl_init (); Curl_setopt ($ ch, CURLOPT_URL, $ url ); Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); // Set curl to IPv4 by default If (defined ('curlopt _ ipresolve') & defined ('curl _ IPRESOLVE_V4 ')){ Curl_setopt ($ ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); } // Sets the maximum number of seconds for a curl request to connect. If it is set to 0, it is unlimited. Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout ); // Sets the maximum number of seconds for curl to execute the action. If it is set to 0, it is unlimited. Curl_setopt ($ ch, CURLOPT_TIMEOUT, $ timeout * 3 ); $ File_contents = curl_exec ($ ch ); Curl_close ($ ch ); |
Note: curl_setopt ($ ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4) takes effect only when php version 5.3 or later, curl version 7.10.8 or later.