file_get_contents介绍和 file_get_contents 实现curl 功能 增加超时的时间限制

来源:互联网 发布:骚气的诗词 知乎 编辑:程序博客网 时间:2024/04/30 16:42
<?php

$postdata 
http_build_query(
    array(
        
'var1' => 'some content',
        
'var2' => 'doh'
    
)
);

$opts = array('http' =>
    array(
        
'method'  => 'POST',
        
'header'  => 'Content-type: application/x-www-form-urlencoded',
        
'content' => $postdata
    
)
);

$context stream_context_create($opts);

$result file_get_contents('http://example.com/submit.php'false$context);

?>

file_get_contents

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

file_get_contents将整个文件读入一个字符串

说明 ¶

string file_get_contents ( string$filename [,bool$use_include_path = false [,resource$context [, int$offset = -1 [,int$maxlen ]]]] )

file() 一样,只除了file_get_contents() 把文件读入一个字符串。将在参数offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败,file_get_contents() 将返回FALSE

file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。

Note:

如果要打开有特殊字符的 URL (比如说有空格),就需要使用 urlencode() 进行 URL 编码。


参数 ¶

filename

要读取的文件的名称。

use_include_path

Note:

As of PHP 5 the FILE_USE_INCLUDE_PATH can be used to triggerinclude path search.

context

A valid context resource created with stream_context_create(). 如果你不需要自定义 context,可以用NULL 来忽略。

offset

The offset where the reading starts on the original stream.

Seeking (offset) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream.

maxlen

Maximum length of data read. The default is to read until end of file is reached. Note that this parameter is applied to the stream processed by the filters.



===========================================

一、增加超时的时间限制

这里需要注意: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'=>60,
)
);

$context = stream_context_create($opts);

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

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

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

$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE) $cnt++;

 

以上方法对付超时已经OK了。

这里会出现一个潜在的错误:如果文件不存在的话,程序将进入死循环,因此,加入判断次数。假设只检查4次。

以下是代码:

$i=0;
while(!$content or $i==4){
  @$content =file_get_contents($url);
  $i++;
}
if($i==4) exit("next");


4次之后还没取到,直接进入下一个函数吧!上面只是终止全部操作,这里的函数可以自行配置

 

那么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' => 'test@gmail.com',
'submit' => 'submit',
);

echo Post('http://www.updateweb.cn', $data);

OK , 上面函数完美了,既解决了超时控制又解决了Post传值。再配合康盛的改良版RC4加密解密算法,做一个安全性很高的webservice就简单多了。






0 0