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

Source: Internet
Author: User
Tags curl

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.