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秒。  



curl普通秒级超时: 

Php代码   收藏代码
  1. $ch = curl_init();  
  2. curl_setopt($ch, CURLOPT_URL,$url);  
  3. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  
  4. curl_setopt($ch, CURLOPT_TIMEOUT,60);   //只需要设置一个秒的数量就可以  
  5. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
  6. curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);  




curl普通秒级超时使用: 
Php代码   收藏代码
  1. curl_setopt($ch, CURLOPT_TIMEOUT,60);  




curl如果需要进行毫秒超时,需要增加: 
Php代码   收藏代码
  1. curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L);  
  2. //或者是:  
  3. curl_setopt ( $ch,  CURLOPT_NOSIGNAL,true);//是可以支持毫秒级别超时设置的  




curl一个毫秒级超时的例子: 

Php代码   收藏代码
  1. if(!isset($_GET['foo'])){  
  2.         // Client  
  3.         $ch = curl_init('http://example.com/');  
  4.         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);  
  5.         curl_setopt($ch, CURLOPT_NOSIGNAL,1);    //注意,毫秒超时一定要设置这个  
  6.         curl_setopt($ch, CURLOPT_TIMEOUT_MS,200);  //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用  
  7.         $data = curl_exec($ch);  
  8.         $curl_errno = curl_errno($ch);  
  9.         $curl_error = curl_error($ch);  
  10.         curl_close($ch);  
  11.    
  12.         if($curl_errno >0){  
  13.                 echo "cURL Error ($curl_errno): $curl_error\n";  
  14.         }else{  
  15.                 echo "Data received: $data\n";  
  16.         }  
  17. }else{  
  18.         // Server  
  19.         sleep(10);  
  20.         echo "Done.";  
  21. }  


其他一些技巧: 
1. 按照经验总结是:cURL 版本 >= libcurl/7.21.0 版本,毫秒级超时是一定生效的,切记。 
2. curl_multi的毫秒级超时也有问题。。单次访问是支持ms级超时的,curl_multi并行调多个会不准 





二、流处理方式访问HTTP  

除了curl,我们还经常自己使用fsockopen、或者是file操作函数来进行HTTP协议的处理,所以,我们对这块的超时处理也是必须的。 



一般连接超时可以直接设置,但是流读取超时需要单独处理。 

自己写代码处理: 
Php代码   收藏代码
  1. $tmCurrent = gettimeofday();  
  2.        $intUSGone =($tmCurrent['sec']- $tmStart['sec'])*1000000  
  3.                   +($tmCurrent['usec']- $tmStart['usec']);  
  4.        if($intUSGone > $this->_intReadTimeoutUS){  
  5.            returnfalse;  
  6.        }  




或者使用内置流处理函数 stream_set_timeout() 和 stream_get_meta_data() 处理: 
Php代码   收藏代码
  1. // Timeout in seconds   
  2. $timeout =5;   
  3. $fp = fsockopen("example.com",80, $errno$errstr$timeout);if($fp){   
  4.         fwrite($fp,"GET / HTTP/1.0\r\n");   
  5.         fwrite($fp,"Host: example.com\r\n");   
  6.         fwrite($fp,"Connection: Close\r\n\r\n");   
  7.         stream_set_blocking($fp,true);   //重要,设置为非阻塞模式  
  8.         stream_set_timeout($fp,$timeout);   //设置超时  
  9.         $info = stream_get_meta_data($fp);   
  10.         while((!feof($fp))&&(!$info['timed_out'])){   
  11.                 $data .= fgets($fp,4096);   
  12.                 $info = stream_get_meta_data($fp);   
  13.                 ob_flush;   
  14.                 flush();   
  15.         }   
  16.         if($info['timed_out']){   
  17.                 echo "Connection Timed Out!";   
  18.         }else{   
  19.                 echo $data;   
  20.         }}  


file_get_contents 超时: 

Php代码   收藏代码
  1. $timeout = array(  
  2.     'http'=> array(  
  3.         'timeout'=>5//设置一个超时时间,单位为秒  
  4.     )  
  5. );  
  6. $ctx = stream_context_create($timeout);  
  7. $text = file_get_contents("http://example.com/",0, $ctx);  




fopen 超时: 
Php代码   收藏代码
  1. $timeout = array(  
  2.    'http' => array(  
  3.        'timeout' => 5 //设置一个超时时间,单位为秒  
  4.    )  
  5. );  
  6.   
  7. $ctx = stream_context_create($timeout);  
  8.   
  9. if ($fp = fopen("http://example.com/""r", false, $ctx)) {  
  10.  while$c = fread($fp, 8192)) {  
  11.    echo $c;  
  12.  }  
  13.  fclose($fp);  
  14. }  

http://zccst.iteye.com/blog/1845958


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值