Curl Timeout Time setting

Source: Internet
Author: User
Tags http authentication php programming

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.