Reason:
If IPv6 is enabled, curl will give priority to parsing IPv6 by default. If the corresponding domain name does not have IPv6, it will wait for the IPv6 dns resolution failure timeout and then follow the previous normal process to find IPv4. In the program, I have set strict timeout limits for curl to obtain content, so it will cause the problem of being unable to obtain content.
Solution: Set the default access to ipv4.
The curl setting method of PHP is as follows:
<?<span php </span><span /*</span><span * * IPV6下curl超时问题 * edit by www.jbxue.com </span><span */</span> <span $ch</span> =<span curl_init(); curl_setopt (</span><span $ch</span>, CURLOPT_URL, <span $url</span><span ); curl_setopt (</span><span $ch</span>, CURLOPT_RETURNTRANSFER, <span true</span><span ); </span><span //</span><span 设置curl默认访问为IPv4</span> <span if</span>(<span defined</span>('CURLOPT_IPRESOLVE') && <span defined</span>('CURL_IPRESOLVE_V4'<span )){ curl_setopt(</span><span $ch</span>, CURLOPT_IPRESOLVE,<span CURL_IPRESOLVE_V4); } </span><span //</span><span 设置curl请求连接时的最长秒数,如果设置为0,则无限</span> curl_setopt (<span $ch</span>, CURLOPT_CONNECTTIMEOUT, <span $timeout</span><span ); </span><span //</span><span 设置curl总执行动作的最长秒数,如果设置为0,则无限</span> curl_setopt (<span $ch</span>, CURLOPT_TIMEOUT,<span $timeout</span>*3<span ); </span><span $file_contents</span> = curl_exec(<span $ch</span><span ); curl_close(</span><span $ch</span>);
Note: curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4) The above settings only take effect when php version 5.3 and above, curl version 7.10.8 and above.