Php solves ipv6 and causes curl to get webpage content timeout solution

Source: Internet
Author: User
Cause: If IPv6 is enabled, curl will resolve IPv6 first by default. if the corresponding domain name does not have IPv6, wait until IPv6dns resolution fails timeout before looking for IPv4 in the previous normal process, in the program... 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:

$ Ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); // set the default curl access to IPv4 if (defined ('curlopt _ ipresolve') & defined ('curl _ IPRESOLVE_V4 ') {curl_setopt ($ ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);} // sets the maximum number of seconds for curl request connection. if it is set to 0, the value of curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout) is unlimited ); // sets the maximum number of seconds for curl to execute the action. if it is set to 0, the value of curl_setopt ($ ch, CURLOPT_TIMEOUT, $ timeout * 3) is infinite; $ 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.


Link to this article:

Add to favorites ^ please keep the tutorial address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.