php curl access to web content of IPV6 timeout solution

  • 2020-07-21 07:18:09
  • OfStack

The reason:
In my program, I put a strict timeout limit on curl's access to content, so there's a problem with not being able to access content.

Solution: Set default access to ipv4.
The curl setting method of php is as follows:


<?php
/**
* IPV6 Under the curl Timeout problems 
*/
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
// Set up the curl Default access is IPv4
if(defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')){
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
// Set up the curl The maximum number of seconds to request a connection if set to 0 , the infinite 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// Set up the curl Maximum number of seconds of total execution action, if set to 0 , the infinite 
curl_setopt ($ch, CURLOPT_TIMEOUT,$timeout*3);
$file_contents = curl_exec($ch);
curl_close($ch);

Note: curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4) will only take effect if php version 5.3 and above, curl version 7.10.8 and above.


Related articles: