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 资料到浏览器前所送出的 ...
随机推荐
- sqlserver2008一直显示正在还原
sqlserver2008一直显示正在还原.如果不需要还原,则使用: restore database test with recovery如果只需要还原,则使用: restore databas ...
- 让你的PHP程序真正的实现多线程(PHP多线程类)
通过WEB服务器来实现PHP多线程功能. 当然,对多线程有深入理解的人都知道通过WEB服务器实现的多线程只能模仿多线程的一些效果,并不是真正意义上的多线程. 但不管怎么样,它还是能满足我们的一些需要的 ...
- CSS样式基础二
一.列表 主要分为有序列表ol,无序列表ul <ul> <li></li> ... </ul> <ol> <li></li ...
- 移动APP项目优化
团队计划:设计一款给用户提供就医帮助的安卓APP. 项目计划:两个月内团队成员共同开发完成此款APP,此款APP提供预约挂号,名医名院咨询,就医导航等功能. 角色职责:负责交互设计.UI界面设计.1. ...
- javascirpt对象运用与JS变量
abcdefghijklmnopqrstuvwyz String 对象方法 charAt() 方法可返回指定位置的字符.stringObject.charAt(index)(index从0开始)[ht ...
- Discuz!模板解析语法
<!--{eval echo autostart("); }--> PHP中使用template()函数显示已存在模板 在Discuz!程序执行中可以通过 include tem ...
- HDU5795A Simple Nim SG定理
A Simple Nim Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 深入了解try catch
@Test public void testWeiXin(){ System.out.println(task()); } private boolean task(){ try { int i = ...
- Creating Dialogs
#ifndef DIALOG_H #define DIALOG_H #include <QtWidgets> //#include <QDialog> //#include & ...
- eclipse加载maven工程提示pom.xml无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3解决方案
pom文件提示信息: Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3 from http:/ ...