源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-01-03 01:59 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解PHP内置访问资源的超时时间 time_out file_get_contents read_file
[b]提问 [/b]我循环用file_get_contents抓取一堆url,但总是会在不到第100个URL的时候停下,提示我:“Warning: file_get_contents(URL) [function.file-get- contents]: failed to open stream: HTTP request failed! HTTP/1.0 500 Read timed out in D:\website\extra.php on line 65” 我在程序的开始已经有set_time_limit(0);了啊,那上面的错误会是因为什么呢? [b]回答 [/b]set_time_limit只是设置你的PHP程序的超时时间,而不是file_get_contents函数读取URL的超时时间。 从警告信息来看,是被抓取的网页出现了服务器500错误,可能是他的程序出现超时了。 如果想改变file_get_contents的超时时间,可以用resource $context的timeout参数:
[url=http://www.php.net/manual/en/context.http.php]http://www.php.net/manual/en/context.http.php [/url][b]附:目前我知道的支持context参数的php内置函数有[/b]file_get_contents,file_put_contents,readfile,file,fopen,copy(估计这一类的函数都支持吧,待确认)。
[u]复制代码[/u] 代码如下:
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.yifu.info', $data);
OK , 上面函数完美了,既解决了超时控制又解决了Post传值。再配合康盛的改良版RC4加密解密算法,做一个安全性很高的webservice就简单多了。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部