Phpcurl timeout settings

Source: Internet
Author: User
Phpcurl timeout settings
This article describes how to set timeout when using curl in php. For more information, see.

There are many HTTP access methods. you can use curl, socket, file_get_contents () and other methods.

Timeout must be considered when accessing http.

1. CURL access http curl is a lib Library commonly used to access HTTP protocol interfaces. it features high performance and some concurrent support functions.

Curl_setopt ($ ch, opt) can be used to set some timeout settings, including: * (important) CURLOPT_TIMEOUT to set 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 is the waiting time before the connection is initiated. if it is set to 0, it will wait infinitely. CURLOPT_CONNECTTIMEOUT_MS indicates the waiting time for the connection attempt, 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:

$ 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 common timeout in seconds:

Curl_setopt ($ ch, CURLOPT_TIMEOUT, 60 );

3. curl needs to be added if millisecond timeout is required:

Curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L); // or curl_setopt ($ ch, CURLOPT_NOSIGNAL, true); // supports millisecond-level timeout settings

Example of curl timeout setting.

1. example of curl timeout in milliseconds:

     0) {echo "cURL Error ($ curl_errno): $ curl_error \ n";} else {echo "Data already ed: $ data \ n ";}} else {// Server sleep (10); echo "Done. ";}

Tip: 1. cURL version> = libcurl/7.21.0. millisecond-level timeout must take effect. remember. 2. for curl_multi's millisecond-level timeout problem, single access supports ms-level timeout, and multiple concurrent calls of curl_multi are not allowed.

2. in addition to curl, stream processing methods often use fsockopen or file operation functions to process HTTP. The following describes the timeout settings.

Generally, connection timeout can be set directly, but stream read timeout needs to be handled separately. You can refer to the following implementation code:

      $this->_intReadTimeoutUS){             returnfalse;         }  

Or use the built-in stream processing functions stream_set_timeout () and stream_get_meta_data () for processing:

     

File_get_contents timeout:

      Array ('timeout' => 5 // set a timeout time in seconds); $ ctx = stream_context_create ($ timeout); $ text = file_get_contents ("http://example.com /", 0, $ ctx );

Fopen timeout:

      Array ('timeout' => 5 // set a timeout time in seconds); $ ctx = stream_context_create ($ timeout ); if ($ fp = fopen ("http://example.com/", "r", false, $ ctx) {while ($ c = fread ($ fp, 8192 )) {echo $ c;} fclose ($ fp );}

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.