首頁  >  文章  >  後端開發  >  如何進行php curl超時設置

如何進行php curl超時設置

藏色散人
藏色散人原創
2020-07-28 10:49:0210760瀏覽

php curl逾時設定的方法:1、使用「curl_setopt($ch, opt)」進行一些逾時的設定;2、使用「CURLOPT_DNS_CACHE_TIMEOUT」設定在記憶體中保存DNS資訊的時間。

如何進行php curl超時設置

推薦:《PHP教學

在php中使用curl時,進行逾時設定的詳細方法

存取HTTP方式很多,可以使用curl, socket, file_get_contents() 等方法。

在訪問http時,需要考慮逾時的問題。

一、CURL 存取HTTP

CURL 是常用的存取HTTP協定介面的lib庫,效能高,還有一些同時支援的功能等。

curl_setopt($ch, opt) 可以設定一些超時的設置,主要包括:

*(重要) CURLOPT_TIMEOUT 設定cURL允許執行的最長秒數。 

*(重要) CURLOPT_TIMEOUT_MS 設定cURL允許執行的最長毫秒數。

(在cURL 7.16.2中被加入。從PHP 5.2.3起可使用)

CURLOPT_CONNECTTIMEOUT 在發起連線前等待的時間,如果設定為0,則無限等待。 

CURLOPT_CONNECTTIMEOUT_MS 嘗試連線等待的時間,以毫秒為單位。如果設定為0,則無限等待。

(在cURL 7.16.2中被加入。從PHP 5.2.3開始可用)

CURLOPT_DNS_CACHE_TIMEOUT 設定在記憶體中儲存DNS資訊的時間,預設為120秒。

1,curl普通秒級超時:

$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普通秒級超時使用:

curl_setopt($ch, CURLOPT_TIMEOUT,60);

3,curl如果需要進行毫秒超時,需要增加:

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

curl逾時設定的範例。

1,curl一個毫秒超時的範例:

<?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.";  
}

技巧:

1,cURL 版本>= libcurl/7.21.0 版本,毫秒超時是一定生效的,切記。

2,curl_multi的毫秒超時的問題,單次存取是支援ms級超時的,curl_multi並行調多個會不準。

二、流處理方式存取HTTP

除了curl,也常使用fsockopen、或file操作函數來進行HTTP協定的處理。

下面說說這方面的超時設定。

一般連接逾時可以直接設置,但是流讀取超時需要單獨處理。

可以參考以下實作程式碼:

<?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;  
       }

或使用內建串流處理函數stream_set_timeout() 和stream_get_meta_data() 處理:

<?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 逾時:

<?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 逾時:

<?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);  
}

以上是如何進行php curl超時設置的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn