0 0

PHP 中file_get_contents 超时时间5

file_get_contents 默认超时时间是多久?
手动设置时间的话可以超过 max_execution_time 吗
PHP 
2012年7月09日 10:47

2个答案 按时间排序 按投票排序

0 0

file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。
通过php.ini中的default_socket_timeout设置,默认超时时间是default_socket_timeout = 60

max_execution_time = 30
default_socket_timeout = 60
假设你使用file_get_contents花费45,而max_execution_time是30,它将超时吗?
答案是NO,因为max_execution_time不影响操作系统调用或stream操作
另一点要指出的的是default_socket_timeout是在socket响应之前计算的,只要得到响应,将会一直执行下去

可以通过以下三种方式设置
1 直接在php.ini中修改  default_socket_timeout =120
2 ini_set('default_socket_timeout',    120);  
3 $strm = stream_context_create(array(
    'http' => array(
        'timeout' => 120
        )
    )
);
file_get_contents("http://www.example.com/", 0, $strm);

2012年7月09日 12:41
0 0


如果想改变file_get_contents的超时时间,可以用resource $context的timeout参数:
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'timeout'=>60,
   )
);

$context = stream_context_create($opts);
$html =file_get_contents('http://www.example.com', false, $context);
fpassthru($fp);

max_execution_time 是设置你的PHP程序的超时时间,所以file_get_contents函数的超时时间不能超过max_execution_time的吧。。。

2012年7月09日 11:01

相关推荐

    PHP file_get_contents 函数超时的几种解决方法

    这里就简单介绍两种: 一、增加超时的时间限制 这里需要注意:set_time_limit只是设置你的PHP程序的超时时间,而不是file_get_contents函数读取URL的超时时间。 我一开始以为set_time_limit也能影响到file_get_...

    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数据。 今天说的这...

    详解PHP内置访问资源的超时时间 time_out file_get_contents read_file

    提问我循环用file_get_contents抓取一堆url,但总是会在不到第100个URL的时候停下,提示我:“Warning: file_get_contents(URL) [function.file-get-contents]: failed to open stream: HTTP request failed...

    深入php函数file_get_contents超时处理的方法详解

    增加超时的时间限制 这里需要注意:set_time_limit只是设置你的PHP程序的超时时间,而不是file_get_contents函数读取URL的超时时间。真正的修改 file_get_contents延时可以用resource $context的timeout参数: 复制...

    PHP中file_get_contents高級用法实例

    本文实例讲述了PHP中file_get_contents高级用法,分享给大家供大家参考。具体分析如下: 首先解决file_get_contents的超时问题,在超时返回错误后就象js中的settimeout那样进行一次尝试,错误超过3次或者5次后就确认...

    比file_get_contents稳定的curl_get_contents分享

    分享一个实际在用的函数: 复制代码 代码如下: /*比file_get_contents稳定的多!$timeout为超时时间,单位是秒,默认为1s。*/ function curl_get_contents($url,$timeout=1) { $curlHandle = curl_init(); curl_...

    PHP file_get_contents函数读取远程数据超时的解决方法

    在网络状况比较差的情况下file_get_contents函数经常读取远程数据失败。 解决办法如下: 复制代码 代码如下: /*设置超时配合失败之后尝试多次读取,效果比原先好很多*/ $url = ‘https://www.jb51.net’;  $opts = ...

    PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析

    后来,我通过跟踪发现,这类情况的出现,跟 PHP 的 file_get_contents() 函数有着密切的关系。 大、中型网站中,基于 HTTP 协议的 API 接口调用,是家常便饭。PHP 程序员们喜欢使用简单便捷的 file_get_...

    执行、获取远程代码返回:file_get_contents 超时处理的问题详解

    本篇文章是对执行、获取远程代码返回:file_get_contents 超时处理的问题进行了详细的分析介绍,需要的朋友参考下

    探讨file_get_contents与curl效率及稳定性的分析

    做过好多抓取别家网站内容的产品,习惯了使用方便快捷的file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的例子设置了超时,可多数时候不会奏效:复制代码 代码如下:$config[‘context’] = ...

    PHP请求远程地址设置超时时间的解决方法

    php请求远程地址设置超时时间,主要讲解file_get_contents、fopen、curl这三个简单常用函数设置超时时间的方法,一般情况下建议使用curl,性能最好,效率也最高。 1、file_get_contents 请求超时设置 $timeout = ...

    php下载文件超时时间的设置方法

    可以使用curl自己实现一个curl_file_get_contents函数 //CURLOPT_FOLLOWLOCATION TRUE 时将会根据服务器返回 HTTP 头中的 "Location: " 重定向。(注意:这是递归的,"Location: " 发送几次就重定向几次,除非设置...

    PHP stream_context_create()函数的使用示例

    ,file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。 比如说,上篇php教程中gd库实现下载网页所有图片中,第10行: 利用了stream_context_create()设置代理服务器: 复制...

    php之curl设置超时实例

    本文实例讲述了php中curl超时设置方法。分享给大家供大家参考。具体实现方法如下: 访问HTTP方式很多,可以使用curl, socket, file_get_contents() 等方法。 在访问http时,需要考虑超时的问题。 CURL访问...

Global site tag (gtag.js) - Google Analytics