This article mainly introduces the curl setting timeout method in php. The example describes various timeout setting methods in curl, which is very useful, for more information about how to set curl timeout in php, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:
There are many HTTP access methods. you can use curl, socket, file_get_contents () and other methods.
Timeout must be considered when accessing http.
CURL access HTTP:
CURL is a common lib library used to access HTTP interfaces. it features high performance and support for concurrency.
Curl_setopt ($ ch, opt) can be used to set timeout settings, including:
① (Important) CURLOPT_TIMEOUT sets the maximum number of seconds that cURL can be executed.
② (Important) CURLOPT_TIMEOUT_MS sets the maximum number of milliseconds that cURL can be executed.
(Added in cURL 7.16.2. Available from PHP 5.2.3)
③ CURLOPT_CONNECTTIMEOUT: The waiting time before the connection is initiated. if it is set to 0, the waiting time is unlimited.
④ The waiting time for CURLOPT_CONNECTTIMEOUT_MS to connect, in milliseconds. If it is set to 0, the system waits for no limit. (Added in cURL 7.16.2. Available from PHP 5.2.3)
⑤ CURLOPT_DNS_CACHE_TIMEOUT sets the time for saving DNS information in the memory. the default value is 120 seconds.
1. curl common seconds timeout:
The code is as follows:
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 60); // you only need to set the number of seconds.
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers );
Curl_setopt ($ ch, CURLOPT_USERAGENT, $ defined_vars ['http _ USER_AGENT ']);
2. curl common timeout in seconds:
The code is as follows:
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 60 );
3. curl:
The code is as follows:
Curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L );
// Or
Curl_setopt ($ ch, CURLOPT_NOSIGNAL, true); // supports millisecond-level timeout settings
I hope this article will help you with PHP programming.