Home  >  Article  >  Backend Development  >  How to set php curl timeout

How to set php curl timeout

藏色散人
藏色散人Original
2020-07-28 10:49:0210758browse

php curl timeout setting method: 1. Use "curl_setopt($ch, opt)" to set some timeouts; 2. Use "CURLOPT_DNS_CACHE_TIMEOUT" to set the time to save DNS information in memory.

How to set php curl timeout

Recommended: "PHP Tutorial"

in php When using curl, the detailed method of setting timeout

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

When accessing http, you need to consider the timeout issue.

1. CURL accesses HTTP

CURL is a commonly used lib library for accessing the HTTP protocol interface. It has high performance and some concurrent support functions.

curl_setopt($ch, opt) You 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 execute.

(Added in cURL 7.16.2. Available from PHP 5.2.3)

CURLOPT_CONNECTTIMEOUT The time to wait before initiating a connection. If set to 0, it will wait infinitely.

CURLOPT_CONNECTTIMEOUT_MS The time to wait for a connection attempt, in milliseconds. If set to 0, wait infinitely.

(Added in cURL 7.16.2. Available since PHP 5.2.3)

CURLOPT_DNS_CACHE_TIMEOUT Set the time to save DNS information in memory, the default is 120 seconds.

1, curl ordinary second-level timeout:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_TIMEOUT,60);   //只需要设置一个秒的数量就可以 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);

2, curl ordinary second-level timeout use:

curl_setopt($ch, CURLOPT_TIMEOUT,60);

3, if curl needs millisecond timeout, you need to add:

curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L); 
//或者 
curl_setopt ( $ch,  CURLOPT_NOSIGNAL,true);//支持毫秒级别超时设置

Example of curl timeout setting.

1, curl a millisecond timeout example:

<?php
if(!isset($_GET[&#39;foo&#39;])){  
        // Client  
        $ch = curl_init(&#39;http://example.com/&#39;);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);  
        curl_setopt($ch, CURLOPT_NOSIGNAL,1);    //注意,毫秒超时一定要设置这个  
        curl_setopt($ch, CURLOPT_TIMEOUT_MS,200);  //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用  
        $data = curl_exec($ch);  
        $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: $data\n";  
        }  
}else{  
        // Server  
        sleep(10);  
        echo "Done.";  
}

Tips:

1, cURL version >= libcurl/7.21.0 version, millisecond timeout is certain Effective, remember.

2. Curl_multi’s millisecond-level timeout problem. A single access supports ms-level timeout. Curl_multi’s parallel adjustment will be inaccurate.

2. Access HTTP through stream processing

In addition to curl, fsockopen or file operation functions are often used to process the HTTP protocol.

Let’s talk about the timeout setting in this regard.

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

You can refer to the following implementation code:

<?php
$tmCurrent = gettimeofday();  
       $intUSGone =($tmCurrent[&#39;sec&#39;]- $tmStart[&#39;sec&#39;])*1000000  
                  +($tmCurrent[&#39;usec&#39;]- $tmStart[&#39;usec&#39;]);  
       if($intUSGone > $this->_intReadTimeoutUS){  
           returnfalse;  
       }

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

<?php
// Timeout in seconds   
$timeout =5;   
$fp = fsockopen("example.com",80, $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);   //重要,设置为非阻塞模式  
        stream_set_timeout($fp,$timeout);   //设置超时  
        $info = stream_get_meta_data($fp);   
        while((!feof($fp))&&(!$info[&#39;timed_out&#39;])){   
                $data .= fgets($fp,4096);   
                $info = stream_get_meta_data($fp);   
                ob_flush;   
                flush();   
        }   
        if($info[&#39;timed_out&#39;]){   
                echo "Connection Timed Out!";   
        }else{   
                echo $data;   
        }}

file_get_contents timeout:

<?php
$timeout = array(  
    &#39;http&#39;=> array(  
        &#39;timeout&#39;=>5//设置一个超时时间,单位为秒  
    )  
);  
$ctx = stream_context_create($timeout);  
$text = file_get_contents("http://example.com/",0, $ctx);

fopen timeout:

<?php
$timeout = array(  
   &#39;http&#39; => array(  
       &#39;timeout&#39; => 5 //设置一个超时时间,单位为秒  
   )  
);  
  
$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 the detailed content of How to set php curl timeout. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn