PHP Curl Timeout settings detailed

Source: Internet
Author: User
This article introduces the use of curl in PHP, the time-out setting of the detailed method, you see, we hope to have a certain help.

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.

One, Curl Access HTTP curl is commonly used to access the HTTP protocol interface Lib Library, high performance, there are some features such as concurrency support.

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 perform. (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-level timeout:

$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, curlopt_returntransfer,1); curl_setopt ($ch, curlopt_timeout,60); Only need to set a number of seconds can be curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, curlopt_useragent, $defined _vars[' http_user_agent ');

2,curl normal second time-out use:

curl_setopt ($ch, curlopt_timeout,60);

3,curl If a millisecond timeout is required, you need to add:

Curl_easy_setopt (curl, curlopt_nosignal,1l); or curl_setopt ($ch, curlopt_nosignal,true);//support for millisecond-level timeout settings

Example of Curl timeout setting.

1,curl An example of a millisecond-level timeout:

 
      0) {                  echo "CURL Error ($curl _errno): $curl _error\n";          } else{                  echo "Data received: $data \ n";          }  } else{          //Server          sleep (ten);          echo "done.";  }  

Tip: 1,curl version >= libcurl/7.21.0 version, the millisecond time-out is certain to take effect, remember. 2,curl_multi millisecond time-out problem, a single access is to support MS-level timeouts, Curl_multi parallel tune multiple will not be allowed.

Stream Processing mode access HTTP In addition to curl, the HTTP protocol is often handled using the Fsockopen, or file operation functions. Here's the timeout setting for this.

The general connection timeout can be set directly, but the 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 handler functions Stream_set_timeout () and Stream_get_meta_data () Processing:

     

File_get_contents timeout:

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

fopen Timeout:

 
       Array (         ' timeout ' = 5//Set a time-out, 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.