PHP file_get_contents设置超时处理方法

 更新时间:2013年10月4日 23:36  点击:3294

file_get_contents的超时处理

话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。

今天说的这篇是讲超时的,确实在跨服务器提交的时候,不可避免的会遇到超时的情况,这个时候怎么办?set_time_limit是没有用的,只有用context中的timeout时间来控制。相反,我们不是要抑止,而是要管理。比如在超时返回错误后,进行一次尝试,就象js中的 settimeout那样,对函数重新处理。错误超过3次或者5次后,我们就确实的认为无法连接服务器而彻底放弃。这,是一个好办法,应该值得推荐使用。其实。不全是file_get_contents,只要支持context的都应该加上,避免超时浪费时间。这样可以被支持的函数大致有:fsocketopen(该函数的最后一个参数。好象比较推荐在读stream的时候,使用stream_time_out函数进行控制),fopen(也是从PHP5开始加入context支持),file(PHP5加入支持),curl(curl有自已的变量 CURLOPT_TIMEOUT)等 。

在使用file_get_contents函数的时候,经常会出现超时的情况,在这里要通过查看一下错误提示,看看是哪种错误,比较常见的是读取超 时,这种情况大家可以通过一些方法来尽量的避免或者解决。这里就简单介绍两种:

一、增加超时的时间限制

这里需要注意:set_time_limit只是设置你的PHP程序的超时时间,而不是file_get_contents函数读取URL的超时时 间。

我一开始以为set_time_limit也能影响到file_get_contents,后来经测试,是无效的。真正的修改 file_get_contents延时可以用resource $context的timeout参数:

复制代码 代码如下:

$opts = array(  
  'http'=>array(  
    'method'=>"GET",  
    'timeout'=>1,//单位秒 
   )  
);   

 $cnt=0;  
while($cnt<3 && ($bb=file_get_contents("http://www.jb51.net", false, stream_context_create($opts)))===FALSE) $cnt++;  
echo $cnt;  
echo $bb; 

二、一次有延时的话那就多试几次

有时候失败是因为网络等因素造成,没有解决办法,但是可以修改程序,失败时重试几次,仍然失败就放弃,因为file_get_contents()如果失 败将返回 FALSE,所以可以下面这样编写代码:

复制代码 代码如下:

$cnt=0;  
 while($cnt<3 && ($bb=file_get_contents("http://www.jb51.net", false, stream_context_create($opts)))===FALSE) $cnt++;

以上方法对付超时已经OK了。那么Post呢?细心点有人发现了'method'=>”GET”, 对!是不是能设置成post呢?百度找了下相关资料,还真可以!而且有人写出了山寨版的post传值函数,如下:

复制代码 代码如下:

function Post($url, $post = null){  
    $context = array ();  
    if (is_array ( $post )) {  
        ksort ( $post );  
        $context ['http'] = array (  
            'timeout' => 60,   
            'method' => 'POST',   
            'content' => http_build_query( $post, '', '&' )  
         );  

    }  
    return file_get_contents ( $url, false, stream_context_create ( $context ) );  
}  

 $data = array (  
    'name' => 'test',  
    'email' => 'admin@admin.com',  
    'submit' => 'submit',  
);  
echo Post ( 'http://www.jb51.net', $data ); 

OK , 上面函数完美了,既解决了超时控制又解决了Post传值。

[!--infotagslink--]

相关文章

  • php 中file_get_contents超时问题的解决方法

    file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25
  • php file_get_contents 设置代理抓取页面示例

    file_get_contents函数在php中可以直接打开本地文件也可以直接抓取远程服务器文件,如果简单的采集我们可以使用file_get_contents直接来操作,如果有防采集我们可能需要...2016-11-25
  • php报错file_get_contents(): php_network_getaddresses问题

    本文章来为各位介绍一篇关于file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known...错误解决办法。 昨天,服务器的DN...2016-11-25
  • PHP file_get_contents设置超时处理方法

    file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
  • file_get_contents()获取https出现这个错误Unable to find the wrapper “https”

    下面我们来看一篇关于file_get_contents()获取https出现这个错误Unable to find the wrapper “https”问题的解决办法. file_get_contents()获取https出现这个错...2016-11-25
  • file_put_contents并发性问题解决方案整理

    在使用file_put_contents时会碰到并发性问题了,对于这个问题我们有多种解决方案了,其实锁是小编比较喜欢的解决办法了,当然也有其它办法,具体如下。 解决 办法一,fil...2016-11-25
  • php提示Warning: file_get_contents(): couldn’t resolve

    在使用file_get_contents函数获取远程文件时提示Warning: file_get_contents(): couldn’t resolve错误了,这个我们可以看出是dns的问题,解决办法也简单。 今天在...2016-11-25
  • file_get_contents不能获取带端口的网址

    本文章来给各位同学介绍file_get_contents不能获取带端口的网址解决办法,有需要了解的同学可参考。 先们来了解file_get_contents() 函数,官方介绍说它是把整个...2016-11-25
  • php中file_get_contents和curl_get_contents介绍

    php中file_get_contents和curl_get_contents介绍 有需要的朋友可参考一下。 分享一个实际在用的函数: file_get_contents() 函数是用于将文件的内容读入到一个字符...2016-11-25
  • centos下file_put_contents()无法写入文件的原因及解决方法

    下面小编就为大家带来一篇centos下file_put_contents()无法写入文件的原因及解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2017-04-03
  • php file_put_contents 生成文件

    <html> <head> <title>test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="http://ajax...2016-11-25
  • php curl file_get_contents post方式获取数据

    下面我们在这里来为各位介绍一篇关于php curl file_get_contents post方式获取数据例子,希望文章能够帮助到各位朋友. curl post,file_get_contents post,curl fi...2016-11-25
  • PHP中file_put_contents写入文件的优点

    file_put_contents写入文件在我看到的phper中很少用到了,但小编以前做flash接受数据时就用到了file_put_contents函数了,下面我们来看看file_put_contents写入文件的优...2016-11-25
  • php file_get_contents 函数

    php file_get_contents 函数 file_get_contents ( PHP 4中“ = 4.3.0 , PHP 5中) file_get_contents -读取整个文件转换成字符串 描述 字符串file...2016-11-25
  • php file_get_contents获取百度热词代码

    这是一段很简单的程序利用了php的file_get_contents函数来采集百度的数据,然后通过simplexml_load_String把它数据解析出来,这样数据就保存到了一个数组,我们就可以方便...2016-11-25
  • file file_get_contents HTTP request failed

    /* 我有一个问题,要求从php教程代码的url。我需要调用一个服务,使用从我的php代码的查询字符串。如果我的浏览器中键入一个网址,它工作还算可以,但如果我使用文件获取,内容...2016-11-25
  • php file_put_contents 函数

    php file_put_contents 函数 file_put_contents ( PHP 5中) file_put_contents -写一个字符串到一个文件 描述 国际file_put_contents (字符串$文件名,...2016-11-25
  • file_get_contents实现数据Post数据方法

    file_get_contents() 函数把整个文件读入一个字符串中。 和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。 file_get_contents() 函数是用于将文件...2016-11-25
  • php中file_get_contents函数高级用法

    file_get_contents函数我们通常是拿来对文件操作了,下面一起来看看file_get_contents函数的高级使用方法吧. 首先解决file_get_contents的超时问题,在超时返回错误...2016-11-25
  • php file_get_contents与curl()函数对比

    在php中file_get_contents与curl()函数都可以用来抓取对方网站的数据并保存到本地服务器中,但是总得来讲file_get_contents()效率稍低些,常用失败的情况、curl()效率挺...2016-11-25