The example in this article describes the Curl Timeout setting method in PHP. Share to everyone for your reference. The implementation method is as follows:
There are many ways to access HTTP, such as curl, Socket, file_get_contents ().
When you access HTTP, you need to consider the issue of timeouts.
Curl accesses http:
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:
① (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 allows to execute.
(added in Curl 7.16.2.) Available from PHP 5.2.3)
③curlopt_connecttimeout the time to wait before initiating the connection, or infinite wait if set to 0.
④curlopt_connecttimeout_ms the time, in milliseconds, that the connection is trying to wait. If set to 0, the wait is infinite. (added in Curl 7.16.2.) Available starting from PHP 5.2.3)
⑤curlopt_dns_cache_timeout sets the time to save DNS information in memory by default of 120 seconds.
1. Curl Normal second timeout:
Copy CodeThe 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 to
curl_setopt ($ch, Curlopt_httpheader, $headers);
curl_setopt ($ch, curlopt_useragent, $defined _vars[' http_user_agent ');
2. Curl normal second time-out use:
Copy CodeThe code is as follows: curl_setopt ($ch, curlopt_timeout,60);
3. If you need to perform a millisecond timeout, curl needs to increase:
Copy CodeThe code is as follows: curl_easy_setopt (curl, curlopt_nosignal,1l);
Or
curl_setopt ($ch, curlopt_nosignal,true);//support for millisecond-level timeout settings
I hope this article is helpful to everyone's PHP programming.
function Curl_post3 ($url, $postData) {
$postData = Json_encode ($postData);
$curl = Curl_init (); Initialization
curl_setopt ($curl, Curlopt_url, $url); Set URL
curl_setopt ($curl, curlopt_httpauth,curlauth_basic); Setting HTTP authentication Methods
curl_setopt ($curl, curlopt_timeout,3); You only need to set the number of seconds to
curl_setopt ($curl, curlopt_returntransfer,1); Set the return mode of the information obtained by curl_exec
curl_setopt ($curl, curlopt_post,1); Set Send as POST request
curl_setopt ($curl, Curlopt_postfields, $postData); Set the data for the post
curl_setopt ($curl, Curlopt_httpheader, Array (
' Content-type:application/json ',
' Content-length: '. Strlen ($postData))
);
$result = curl_exec ($curl);
Curl_close ($curl);
Return Json_decode ($result, true);
}
Curl Timeout Time setting