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 函数超时的几种解决方法

    如果你发现`file_get_contents`在获取远程URL时超时,需要通过创建自定义的上下文(context)来设置HTTP选项中的超时时间。 ```php $opts = array( 'http' => array( 'method' => "GET", 'timeout' => 60, // ...

    PHP file_get_contents设置超时处理方法

    在PHP中,set_time_limit函数是用来设置脚本执行的最大时间,然而,这个函数并不适用于file_get_contents函数的超时控制。file_get_contents函数读取URL超时是由其他机制控制的。 从PHP 5.0版本开始,file_get_...

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

    总的来说,解决`file_get_contents` 读取远程数据超时的问题,主要通过设置超时时间和采用重试机制,以及在处理大文件时选择更合适的读取方法。理解这些技巧将有助于编写更健壮和高效的PHP代码。

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

    它主要用于设置当前PHP脚本运行的最大时间(以秒为单位),但这个限制并不直接影响 `file_get_contents()` 的超时设置。`file_get_contents()` 的超时是由其内部使用的流上下文(stream context)控制的。 要修改 `...

    file_get_contents获取不到网页内容的解决方法

    如果遇到`file_get_contents`超时,可以尝试增加超时时间,或者使用cURL进行更精细的控制。 总之,当`file_get_contents`遇到获取不到网页内容的问题时,可以考虑使用cURL作为替代,利用其强大的功能和更好的错误...

    php 使用file_get_contents读取大文件的方法

    file_get_contents 函数是 PHP 中内置的一个函数,它能够从指定的文件读取内容,并将内容直接作为一个字符串返回。它非常适合用于读取大文件,因为可以指定从文件中读取的起始位置和读取长度,这样就可以避免一次性...

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

    在描述的问题中,服务器的CPU负载增加,`php-cgi`进程的CPU使用率达到100%,通常是由`file_get_contents()`长时间等待远程响应而引起的。`file_get_contents()`函数在处理HTTP请求时,并不会受到`php.ini`配置文件中...

    比file_get_contents稳定的curl_get_contents分享

    在PHP开发中,我们经常需要从远程服务器获取数据,这时`file_get_contents`和`curl`函数成为常用的选择。本文将详细讨论`file_get_contents`和`curl`的区别,以及为何`curl_get_contents`可能比`file_get_contents`...

    解析PHP中的file_get_contents获取远程页面乱码的问题

    在PHP编程中,`file_get_contents` 是一个非常实用的函数,用于从本地或远程位置读取文件内容。然而,当尝试获取的远程页面已经经过gzip压缩时,可能会遇到返回内容显示为乱码的问题。这是因为`file_get_contents`...

    解决file_get_contents无法请求https连接的方法

    在PHP编程中,`file_get_contents` 是一个非常实用的函数,可以用来读取本地或远程文件内容。然而,当尝试使用 `file_get_contents` 函数读取HTTPS(安全的HTTP)连接时,可能会遇到一些问题。这主要是由于PHP默认...

    PHP中使用file_get_contents post数据代码例子

    file_get_contents() 默认是通过GET方法获取数据,但通过适当配置,它也可以用来通过POST方法发送数据。 上述内容提到的代码例子主要演示了如何使用file_get_contents()函数来向指定URL发送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中使用Curl、socket、file_get_contents三种方法POST提交数据

    在PHP中,获取远程内容的方法主要有三种:Curl、socket和`file_get_contents`。每种方法都有其独特的特性和适用场景。以下是这三种方法的详细介绍: 1. **Curl (Client URL Library)** Curl 是一个强大的命令行...

    PHP中file_get_contents高級用法实例

    真正能够控制file_get_contents超时时间的是使用stream_context_create()函数创建的上下文环境。通过设置"context"数组中的'http'部分的'timeout'键值,我们可以指定超时时间。例如,可以将超时时间设置为60秒。这样...

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

    针对 `file_get_contents()` 的超时问题,我们可以利用其第三个参数 `context` 来设置超时时间。例如: ```php $url = "http://zhoz.com/zhoz.php"; $ctx = stream_context_create(array( 'http' => array( 'time...

    php中file_get_contents()函数用法实例

    file_get_contents()函数是PHP中的一个内置函数,主要用于将文件或远程URL的内容读取到字符串变量中。它是一个简单而强大的函数,常用于获取数据、读取文件内容、获取网页内容等场景。在深入探讨file_get_contents()...

Global site tag (gtag.js) - Google Analytics