PHP内置访问资源的超时时间 time_out file_get_contents read_file的解决办法
内容摘要
这篇文章主要为大家详细介绍了PHP内置访问资源的超时时间 time_out file_get_contents read_file的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起
感兴趣的小伙伴,下面一起
文章正文
这篇文章主要为大家详细介绍了PHP内置访问资源的超时时间 time_out file_get_contents read_file的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!
提问我循环用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 outin D:\website\extra.php on line 65”我在程序的开始已经有set_time_limit(0);了啊,那上面的错误会是因为什么呢?回答set_time_limit只是设置你的PHP程序的超时时间,而不是file_get_contents函数读取URL的超时时间。从警告信息来看,是被抓取的网页出现了服务器500错误,可能是他的程序出现超时了。如果想改变file_get_contents的超时时间,可以用resource $context的timeout参数:代码如下:
1 2 3 4 5 6 7 8 9 10 | <code> $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 ); </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <code> 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 ); </code> |
注:关于PHP内置访问资源的超时时间 time_out file_get_contents read_file的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释