php file_get_contents() 用法
php 需要访问某个网页
知识扩充
file_get_contents()模拟referer,cookie, 使用proxy等等
参考代码
ini_set('default_socket_timeout',120);
ini_set('user_agent','MSIE 6.0;');
$context=array('http' => array ('header'=> 'Referer: http://www.baidu.com/', ),);
$xcontext = stream_context_create($context);
echo $str=file_get_contents('http://www.webkaka.com/',FALSE,$xcontext);
首先解决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程序代码如下:
$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程序代码如下:
$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE){
$cnt++;
}
以上方法对付超时已经OK了。接下来演示一下用file_get_contents实现Post,如下:
PHP程序代码
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.www.com', $data);
注意文档头的Set_time_out否则整个文档都得超时了
file_get_contents("php://input")的使用方法
http://www.kuitao8.com/20140727/2867.shtml
文档部分转载http://www.jb51.net/article/55574.htm
php file_get_contents() 用法的更多相关文章
- 分享一个强大的采集类,还可以模拟php多进程
做采集的时候,可以使用file_get_contents()去获取网页源代码,但是使用file_get_contents采集,速度慢,而且超时时间,不好控制.如果采集的页面不存在,需要等待的时间很长. ...
- php中的 file_get_contents(‘php://input’)用法
php中的 file_get_contents('php://input')用法: file_get_contents 获取php页面中input内容的值: eg: php: 页面提交了usernam ...
- file_get_contents高級用法
首先解決file_get_contents的超時問題,在超時返回錯誤後就象js中的settimeout那樣進行一次嘗試,錯誤超過3次或者5次後就確認為無法連線伺服器而徹底放棄.這裡就簡單介紹兩種解決方 ...
- file_get_contents("php://input")的用法
$data = file_get_contents("php://input"); php://input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好使用 ...
- file_put_contents以及file_get_contents的用法与在使用过程中遇到的问题(PHP学习)
对数据的操作最基本的是增删改查,file_put_contents以及file_get_contents是对文件里的数据进行存入与取出. 先上代码: <?php $str = 'hello wo ...
- file_get_contents()/file_put_contents()
PHP file_get_contents() 函数 定义和用法 file_get_contents() 把整个文件读入一个字符串中. 该函数是用于把文件的内容读入到一个字符串中的首选方法.如果服务器 ...
- 解决file_get_contents遇到中文文件名无法打开问题
利用file_get_contents打开文件或采集远程服务器文件如果文名或url中碰到汉字中文那么会出现failed to open stream:Lnvalid argument in错误. ...
- php中的curl使用入门教程和常见用法实例
摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...
- PHP中header用法详解带范例(转)
header的用法 header()函数的作用是:发送一个原始 HTTP 标头[Http Header]到客户端.标头 (header) 是服务器以 HTTP 协义传 HTML 资料到浏览器前所送出的 ...
随机推荐
- org.hibernate.AssertionFailure:collection[......] was not processed by flush()
八月 12, 2016 11:00:49 上午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() f ...
- bzoj3223 文艺平衡树
传送门 :http://www.lydsy.com/JudgeOnline/problem.php?id=3223 splay区间翻转的基础题,然而我还是调了一晚上(蒟蒻的悲哀) #include & ...
- Masonry的使用
1.//添加了这个宏,就不用带mas_前缀了 #define MAS_SHORTHAND //添加了这个宏,equalTo就等于mas_equalTo #define MAS_SHORYHAND_G ...
- 2.[WP Developer体验Andriod开发]Andriod Studio结合Visual Studio Emulator for Android调试Android App
0. 工欲善其事必先利其器 上一篇博客对比了一下Android和WinPhnoe的布局容器,后续篇章重点放在Android的开发上了. 说到开发就绕不开调试程序,调试Android App我们有2种选 ...
- SAML 2.0 setup steps, 效果图
Steps of setting up SAML SSO. 效果图 # Registry a Identity Provider services in:(Might need purchase) I ...
- angular源码阅读的起点,setupModuleLoader方法
angular源码其实结构非常清晰,划分的有条有理的,大概就是这样子: (function(window,document,jquery,undefined){ //一些工具函数 //EXPR 编译器 ...
- 中文 iOS/Mac 开发博客列表(转)
转自https://github.com/tangqiaoboy/iOSBlogCN 中文 iOS/Mac 开发博客列表 本博客列表会不断更新维护,如果有推荐的博客,请到此处提交博客信息. 本博客列表 ...
- repeater留言板[转]
做了一个网站,其中的在线留言板块是用Repeater来显示留言的,这样可以用少的代码还实现多的功能,但是不知道怎么分页,要是留言过多就会使页面变的很长,能过查看众多网友的经验,知道用PagedData ...
- jquery ajax异步请求
得先知道后台接口给ajax访问(接口URl和传入接口的参数及参数类型),知道访问之后返回的数据类型,有哪些数据. 选择异步请求的方式,常用的有三种,如$.ajax().$.post().$.get ...
- Oracle(创建index)
概念: 1. 类似书的目录结构 2. Oracle 的“索引”对象,与表关联的可选对象,提高SQL查询语句的速度 3. 索引直接指向包含所查询值的行的位置,减少磁盘I/O 4. 与所索引的表是相互独立 ...