[HTTP access]
Generally we access HTTP in many ways, mainly: curl, socket, file_get_contents() and other methods.
If the other party's server never responds, we will be in tragedy. It is easy to kill the entire server, so we also need to consider the timeout issue when accessing http.
[CURL access HTTP]
CURL is a commonly used and reliable lib library for accessing the HTTP protocol interface. It has high performance and some concurrency support. functions, etc.
CURL:
curl_setopt($ch, opt) can set some timeout settings, mainly including:
CURLOPT_TIMEOUT Set the maximum number of seconds cURL is allowed to execute.
CURLOPT_TIMEOUT_MS Sets the maximum number of milliseconds cURL is allowed to execute. (Added in cURL 7.16.2. Available from PHP 5.2.3.)
CURLOPT_CONNECTTIMEOUT The time to wait before initiating a connection. If set to 0, it will wait indefinitely.
CURLOPT_CONNECTTIMEOUT_MS The time to wait for a connection attempt, in milliseconds. If set to 0, wait infinitely. Added in cURL 7.16.2. Available starting with PHP 5.2.3.
CURLOPT_DNS_CACHE_TIMEOUT Set the time to save DNS information in memory, the default is 120 seconds.
curl ordinary second-level timeout:
$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']);
curl ordinary second-level timeout usage:
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
If curl requires millisecond timeout, you need to add:
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L );
Or:
curl_setopt ($ch, CURLOPT_NOSIGNAL, true); can support millisecond level timeout settings
An example of a millisecond timeout for curl:
<?php if (!isset($_GET['foo'])) { // Client $ch = curl_init('http://example.com/'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个 curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用 $data = curl_exec($ch); $curl_errno = curl_errno($ch); $curl_error = curl_error($ch); curl_close($ch); if ($curl_errno > 0) { echo "cURL Error ($curl_errno): $curl_error\n"; } else { echo "Data received: $data\n"; } } else { // Server sleep(10); echo "Done."; } ?>
Some other tips:
1. According to experience, the summary is: cURL version >= libcurl/7.21. In version 0, the millisecond timeout will definitely take effect, remember.
2. There is also a problem with the millisecond timeout of curl_multi. . A single access supports ms-level timeout, but curl_multi will be inaccurate if multiple calls are made in parallel.
[Accessing HTTP via stream processing]
In addition to curl, we often also Use fsockopen or file operation function to process the HTTP protocol, so our timeout processing for this is also necessary.
Recommended tutorial: PHP video tutorial
The above is the detailed content of How to solve PHP request interface timeout. For more information, please follow other related articles on the PHP Chinese website!