This article mainly introduces the PHP request remote address setting Timeout time resolution method, the interest friend's reference, hoped to be helpful to everybody.
PHP Request remote Address set time-out, mainly on the file_get_contents, fopen, curl, the three simple common functions to set the time-out method, generally recommended using curl, the best performance, the highest efficiency.
1. file_get_contents Request Timeout setting
$timeout = Array (' http ' = = ' Array (' timeout ' =>5//set a time-out, 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 time-out, in seconds), $ctx = Stream_context_create ($timeout); if ($fp = f Open ("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 that accesses the HTTP protocol interface, with high performance, and some features that are supported concurrently.
curl_setopt ($ch, opt) can set some timeout 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 curl is allowed to perform.
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, to try to connect the wait. If set to 0, the wait is infinite. E, Curlopt_dns_cache_timeout sets the time to save the DNS information in memory, which defaults to 120 seconds.
$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, curlopt_returntransfer,1); curl_setopt ($ch, CURLOPT_TIMEOUT,60); Just set the number of seconds to curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, curlopt_useragent, $defined _vars[' Http_user_agent ']);
The above is the whole content of this article, I hope that everyone's study has helped.