Home  >  Article  >  php教程  >  php之curl设置超时实例,phpcurl实例

php之curl设置超时实例,phpcurl实例

WBOY
WBOYOriginal
2016-06-13 09:22:27903browse

php之curl设置超时实例,phpcurl实例

本文实例讲述了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);//支持毫秒级别超时设置


 
希望本文所述对大家的PHP程序设计有所帮助。

几种常见的PHP超时处理方法

【Web服务器超时处理】

[ Apache ]
一般在性能很高的情况下,缺省所有超时配置都是30秒,但是在上传文件,或者网络速度很慢的情况下,那么可能触发超时操作。
目前apachefastcgiphp-fpm模式下有三个超时设置:
fastcgi超时设置:
修改的fastcgi连接配置,类似如下:

  复制代码 代码如下:

  
FastCgiExternalServer/home/forum/apache/apache_php/cgi-bin/php-cgi-socket/home/forum/php5/etc/php-fpm.sock
ScriptAlias/fcgi-bin/"/home/forum/apache/apache_php/cgi-bin/"
AddHandlerphp-fastcgi.php
Actionphp-fastcgi/fcgi-bin/php-cgi
AddTypeapplication/x-


缺省配置是30s,如果需要定制自己的配置,需要修改配置,比如修改为100秒:(修改后重启apache):

  复制代码 代码如下:

  
FastCgiExternalServer/home/forum/apache/apache_php/cgi-bin/php-cgi-socket/home/forum/php5/etc/php-fpm.sock-idle-timeout100
ScriptAlias/fcgi-bin/"/home/forum/apache/apache_php/cgi-bin/"
AddHandlerphp-fastcgi.php
Actionphp-fastcgi/fcgi-bin/php-cgi
AddTypeapplication/x-


如果超时会返回500错误,断开跟后端php服务的连接,同时记录一条apache错误日志:
[ThuJan2718:30:152011][error][client10.81.41.110]FastCGI:commwithserver"/home/forum/apache/apache_php/cgi-bin/php-cgi"aborted:idletimeout(30sec)
[ThuJan2718:30:152011][error][client10.81.41.110]FastCGI:incompleteheaders(0bytes)receivedfromserver"/home/forum/apache/apache_php/cgi-bin/php-cgi"
其他fastcgi......余下全文>>
 

curl 超时输出

function curl_file_get_contents($url){
$starttime = time();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = trim(curl_exec($ch));
curl_close($ch);

$endtime = time();

if($endtime-$starttime>=15){
echo 'php运行超时';
}

return $contents;
}
 

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