A brief talk on the solution of PHP processing backend & interface Access Timeout _php tips

Source: Internet
Author: User
Tags curl

"HTTP Access"

Generally we visit HTTP way many, mainly is: curl, Socket, file_get_contents () and other methods.

If you encounter the other server has not been responding, we are tragic, it is easy to kill the entire server, so when accessing HTTP also need to consider the problem of timeout.

[CURL Access HTTP]

CURL is one of the most reliable LIB libraries for accessing HTTP protocol interfaces, with high performance and some concurrent support functions.

CURL:

curl_setopt ($ch, opt) can set some time-out settings, mainly including:

* (important) Curlopt_timeout sets the maximum number of seconds that the curl allows to execute.

* (important) Curlopt_timeout_ms sets the maximum number of milliseconds that the curl allows to execute. (Joined in the Curl 7.16.2.) Available from PHP 5.2.3. )

Curlopt_connecttimeout the time to wait before initiating the connection, and if set to 0, wait indefinitely.

Curlopt_connecttimeout_ms the time, in milliseconds, that the attempt to connect waits.  If set to 0, wait indefinitely. Be joined in the 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.

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); You just need to set a number of seconds to

curl_setopt ($ch, Curlopt_httpheader, $headers);

curl_setopt ($ch, curlopt_useragent, $defined _vars[' http_user_agent '));

Curl Normal Second level timeout usage:

curl_setopt ($ch, curlopt_timeout, 60);

Curl If you need to do a millisecond timeout, you need to increase:

Curl_easy_setopt (Curl, curlopt_nosignal, 1L);

Or is:

curl_setopt ($ch, curlopt_nosignal, true); That can support the millisecond level timeout setting.

Curl example of a millisecond timeout:

<?php
if (!isset ($_get[' foo ')) {
    
//Client
    $ch = curl_init (' http://example.com/');
    curl_setopt ($ch, Curlopt_returntransfer, true);
    curl_setopt ($ch, curlopt_nosignal, 1);  
Note that the millisecond timeout must be set for this
    curl_setopt ($ch, Curlopt_timeout_ms, MB); 
Timeout millisecond, joined in CURL 7.16.2.
    $data = curl_exec ($ch)
    can be used from PHP 5.2.3. $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: $da Ta\n ";
    }
} else {
    
//Server sleep
    ;
    echo "done.";
}
? >

Some other tips:

1. According to the experience summary is: CURL version >= libcurl/7.21.0 version, the millisecond timeout is certain to be effective, remember.

2. Curl_multi has a problem with the millisecond timeout. Single access is to support MS level timeout, Curl_multi parallel multiple will not be allowed

[Stream processing Access HTTP]

In addition to curl, we often use the fsockopen, or the file operation function to deal with the HTTP protocol, so we are also necessary for this block of timeout processing.

The general connection timeout can be set directly, but the stream read timeout needs to be handled separately.

Write your own code processing:

$tmCurrent = Gettimeofday ();
$intUSGone = ($tmCurrent [' sec ']-$tmStart [' sec ']) * 1000000
+ ($tmCurrent [' usec ']-$tmStart [' usec ']);
if ($intUSGone > $this->_intreadtimeoutus) {return
false;
}

or use the built-in stream handler functions Stream_set_timeout () and Stream_get_meta_data () Processing:

<?php 
//Timeout in seconds 
$timeout = 5; 
$fp = Fsockopen ("example.com", $errno, $errstr, $timeout); 
if ($fp) { 
    fwrite ($fp, "get/http/1.0\r\n"); 
    Fwrite ($fp, "host:example.com\r\n"); 
    Fwrite ($fp, "connection:close\r\n\r\n"); 
    Stream_set_blocking ($fp, true);  
Important, set to non-blocking mode
    stream_set_timeout ($fp, $timeout);  
Set timeout
    $info = Stream_get_meta_data ($fp); 
    while (!feof ($FP)) && (! $info [' timed_out ']) { 
        $data. = Fgets ($fp, 4096); 
        $info = Stream_get_meta_data ($fp); 
        Ob_flush; 
        Flush (); 
    } 
    if ($info [' timed_out ']) { 
        echo "Connection timed out!"; 
    } else { 
        echo $data; 
    } 
}

File_get_contents timeout:

<?php
$timeout = Array ('
  http ' => Array (
    ' timeout ' => 5 
//Set a timeout, in seconds
  )
);
$ctx = Stream_context_create ($timeout);
$text = file_get_contents ("http://example.com/", 0, $ctx);
? >

fopen Timeout:

<?php
$timeout = Array ('
  http ' => Array (
    ' timeout ' => 5 
//Set a timeout, 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);
>

The above is a small series for everyone to talk about PHP processing back-end & Interface Access Timeout solution All content, I hope that we support cloud-Habitat Community ~

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.