`

PHP中file_get_contents高級用法实例

    博客分类:
  • php
 
阅读更多

PHP中file_get_contents高級用法实例

                         转自:http://www.jb51.net/article/55574.htm

首先解决file_get_contents的超时问题,在超时返回错误后就象js中的settimeout那样进行一次尝试,错误超过3次或者5次后就确认为无法连线伺服器而彻底放弃。
这裡就简单介绍两种解决方法:

一、增加超时的时间限制

注意:set_time_limit只是设定你的PHP程式的超时时间,而不是file_get_contents函数读取URL的超时时间。

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

PHP程序代码如下:

1
2
3
4
5
6
7
8
9
10
11
$opts = array(
    'http'=>array(
      'method'=>"GET",
      'timeout'=>60,
    )
);
 
$context = stream_context_create($opts);
 
$html =file_get_contents('http://www.jb51.net', false, $context);
fpassthru($fp);

二、多次尝试

PHP程序代码如下:

1
2
3
4
$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE){
   $cnt++;
}

以上方法对付超时已经OK了。接下来演示一下用file_get_contents实现Post,如下:
PHP程序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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' => 'test@gmail.com',
    'submit' => 'submit',
);
 
echo Post('http://www.jb51.net', $data);

注意文档头的Set_time_out否则整个文档都得超时了。

分享到:
评论

相关推荐

    PHP使用fopen与file_get_contents读取文件实例分享

    其中,fopen()和file_get_contents()是最常用的两种方法。fopen()函数用于打开文件或URL,并返回一个文件句柄,该句柄可以用于读取文件内容。而file_get_contents()函数则提供了一种更简便的方式,直接读取文件或URL...

    php中file_get_contents&#40;&#41;函数用法实例

    下面,我们将根据file_get_contents()函数的参数,介绍几个具体的实例用法。 ### 读取本地文件内容 假设我们有一个名为“input.txt”的文本文件,位于与PHP脚本同一目录下。我们可以使用file_get_contents()函数来...

    file_get_contents&#40;php://input, r&#41;实例介绍

    在本文中,我们将深入探讨 `file_get_contents('php://input', 'r')` 的实例,以及它在处理 POST 数据时的作用。 在 PHP 中,`php://input` 是一个特殊的输入流,它允许我们访问原始的 HTTP 请求主体。这在处理 ...

    php采用file_get_contents代替使用curl实例

    本文实例讲述了php采用file_get_contents代替使用curl的方法,分享给大家供大家参考。具体实现方法如下: file_get_contents代替使用curl其实不多见了,但有时你碰到服务器不支持curl时我们可以使用file_get_...

    PHP curl 或 file_get_contents 获取需要授权页面的方法

    总之,通过这篇文章,我们可以了解到在PHP中如何使用curl扩展和file_get_contents函数来处理需要HTTP基本认证的页面请求,并且掌握如何处理认证失败的情况。这对于进行Web开发和数据集成的PHP开发者来说是十分重要的...

    php基于curl重写file_get_contents函数实例

    主要介绍了php基于curl重写file_get_contents函数的方法,结合实例形式分析了php使用curl重写file_get_contents函数实现屏蔽错误提示的相关技巧,需要的朋友可以参考下

    PHP使用file_get_contents发送http请求功能简单示例

    主要介绍了PHP使用file_get_contents发送http请求功能,结合实例形式分析了file_get_contents结合stream_context_create实现的发送post请求数据相关原理与操作技巧,需要的朋友可以参考下

    PHP使用file_get_content设置头信息的方法

    在本文中,我们将重点介绍如何使用PHP中的file_get_contents函数来设置HTTP头信息,以及如何使用fopen函数配合stream_context来获取或发送HTTP头信息。这两种方法都是在PHP中进行网络请求时常用的技巧,特别是当你...

    php读取本地文件常用函数(fopen与file_get_contents)

    下面我们以.txt文件为实例来介绍php读取本地文件的函数,读取文件我们可以利用fopen或file_get_contents来读取,file_get_contents更简单而fopen需要fread配合才可以显示读出的内容

    php中ob_get_length缓冲与获取缓冲长度实例

    本文实例讲述了php中ob_get_length缓冲与获取缓冲长度的方法。分享给大家供大家参考。具体方法如下: file_get_contents&#40;&#41; 函数把整个文件读入一个字符串中,和 file&#40;&#41; 一样,不同的是 file_get_...

    file_get_contents&#40;&quot;php://input&quot;, &quot;r&quot;&#41;实例介绍

    在本文中,我们将深入探讨 `file_get_contents("php://input", "r")` 的工作原理、使用场景以及如何通过实例来理解这一功能。 `file_get_contents()` 函数的基本语法是 `file_get_contents(filename, context, ...

    php简单检测404页面的方法示例

    使用file_get_contents函数,可以读取web中的网页或者文件。 如果遇到404页面,则会返回false,否则返回相应的网页内容。 使用该函数有两点需要注意: 1.file_get_contents在读取不存在的页面时,会报一个warning,...

    PHP 读写文件操作实例

    在PHP中,读取文件主要使用`file_get_contents()`函数,它能够将整个文件读入一个字符串。例如,我们可以这样读取`list.txt`文件: ```php $content = file_get_contents('list.txt'); echo $content; ``` 写入...

Global site tag (gtag.js) - Google Analytics