HTTP requests have two timeout periods: One is the connection timeout, and the other is the maximum allowable time for the data transfer (request resource time-out).
Use the Curl command-line connection timeout with the--connect-timeout parameter to specify the maximum allowable time for data transfer with the-m parameter specified for example: Curl--connect-timeout 10-m "Http://XXXXXXX" Connection timed out, Error hint form: Curl: (+) Connect () timed out! The maximum allowable time for data transfer expires, the error message is as follows: Curl: (0) operation timed out after the Bytes Received use PHP curl_init connection Timeout curl_setopt ($ch, Curlopt_connecttimeout, 3), maximum allowable time for data transfer curl_setopt ($ch, curlopt _timeout, 3); Use Curl_error ($ch) to view the details of the error as
<?PHP//Create a new CURL resource$ch=curl_init ();//set URL and other appropriate optionscurl_setopt ($ch, Curlopt_url, "http://www.example.com/"); curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_connecttimeout, 3); curl_setopt ($ch, Curlopt_timeout, 30);//grab URL and pass it to the browserCurl_exec ($ch);//Close CURL resource, and free up system resourcesCurl_close ($ch);Var_dump(Curl_error ($ch));
Fsockopen connection time-out using PHP fsockopen ($hostname, $port, $errno, $errstr, $timeout); The maximum allowable time for data transfer is unclear, although it can be stream_set_ Timeout and stream_get_meta_data (which need to be placed after fread) get the data transfer timeout, but cannot stop the request within the set timeout period, so the target page content will stop until the maximum execution time is reached if the Sleep Request page is stopped. Example
<?PHP$fp=Fsockopen("www.example.com", 80);if(!$fp) { Echo"Unable to open\n";} Else { fwrite($fp, "get/http/1.0\r\n\r\n"); Stream_set_timeout($fp, 2); $res=fread($fp, 2000); $info=Stream_get_meta_data($fp); fclose($fp); if($info[' Timed_out ']) { Echo' Connection timed out! '; } Else { Echo $res; }}?>
Using file_get_contents
The maximum allowable time for connection timeout and data transfer is set to timeout, as
<?PHP$timeout= 60; $options=Array( ' http ' =Array( ' Method ' = ' GET ', ' header ' = ' accept-language:en\r\n ', ' timeout ' =$timeout ) ); $context=stream_context_create($options); $contents=file_get_contents($source,false,$context); ?>
file_get_contents analog post and get
functionHttp_request ($url,$post=NULL){ $timeout=30; if(Is_array($post)) {Ksort($post); $options[' http '] =Array(' Timeout ' =$timeout, ' method ' = ' POST ', ' content ' =Http_build_query($post, ', ' & '),); } $context=stream_context_create($options); return file_get_contents($url,false,$context);}
Analog HTTP request time-out setting