Solution to php curl obtaining web page content (timeout under IPV6)_PHP tutorial

WBOY
Release: 2016-07-20 11:12:56
Original
949 people have browsed it

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>);
Copy after login


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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440400.htmlTechArticleReason: 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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template