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, see
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, see
This example describes how to set curl timeout in php. 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.