PHP Request remote Address set timeout, mainly explained file_get_contents, fopen, curl These three simple commonly used functions to set the time-out time method, generally recommended to use curl, the best performance, efficiency is the highest.
1. file_get_contents Request Timeout setting
$timeout = Array ('
http ' => Array (
' timeout ' =>5//set a timeout, in seconds
)
;
$ctx = Stream_context_create ($timeout);
$text = file_get_contents ("http://www.jb51.net/", 0, $ctx);
2. fopen Request Timeout setting
$timeout = Array ('
http ' => Array (
' timeout ' => 5//Set a timeout, in seconds
)
);
$ctx = Stream_context_create ($timeout);
if ($fp = fopen ("http://www.jb51.net/", "R", False, $ctx)) {while
($c = Fread ($fp, 8192)) {
echo $c;
}
fclose ($fp);
3. Curl Request Timeout setting
CURL is a common Lib library to access HTTP protocol interface, which has high performance and some concurrent support functions.
curl_setopt ($ch, opt) can set some time-out settings, mainly including:
A, Curlopt_timeout sets the maximum number of seconds that curl is allowed to execute.
B, Curlopt_timeout_ms sets the maximum number of milliseconds that the curl allows to execute.
C, curlopt_connecttimeout the time to wait before initiating the connection, and if set to 0, wait indefinitely.
D, Curlopt_connecttimeout_ms the time, in milliseconds, that the attempt to connect waits. If set to 0, wait indefinitely. E, Curlopt_dns_cache_timeout set the time to save DNS information in memory by default of 120 seconds.
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer,1);
curl_setopt ($ch, curlopt_timeout,60); Only need to set a number of seconds to
curl_setopt ($ch, Curlopt_httpheader, $headers);
curl_setopt ($ch, curlopt_useragent, $defined _vars[' http_user_agent '));
The above is a small series for everyone to bring the PHP request remote Address set timeout solution for all content, I hope that we support cloud Habitat Community ~