The example in this article describes the curl timeout setting method in PHP. Share it with everyone for your reference. The specific implementation method is as follows:
There are many ways to access HTTP, you can use curl, socket, file_get_contents() and other methods.
When accessing http, you need to consider the issue of timeout.
CURL access HTTP:
CURL is a commonly used lib library for accessing HTTP protocol interfaces. It has high performance and has some concurrency support functions.
curl_setopt($ch, opt) can set some timeout settings, mainly including:
① (Important) CURLOPT_TIMEOUT sets the maximum number of seconds that cURL is allowed to execute.
② (Important) CURLOPT_TIMEOUT_MS sets the maximum number of milliseconds that cURL is allowed to execute.
(Added in cURL 7.16.2. Available from PHP 5.2.3 onwards)
③ CURLOPT_CONNECTTIMEOUT is the time to wait before initiating a connection. If set to 0, it will wait indefinitely.
④ CURLOPT_CONNECTTIMEOUT_MS The time to wait for trying to connect, in milliseconds. If set to 0, wait infinitely. (Added in cURL 7.16.2. Available since PHP 5.2.3)
⑤ CURLOPT_DNS_CACHE_TIMEOUT sets the time to save DNS information in memory, the default is 120 seconds.
1. curl normal second-level timeout:
Copy code
Copy code