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() 用法的更多相关文章

  1. 分享一个强大的采集类,还可以模拟php多进程

    做采集的时候,可以使用file_get_contents()去获取网页源代码,但是使用file_get_contents采集,速度慢,而且超时时间,不好控制.如果采集的页面不存在,需要等待的时间很长. ...

  2. php中的 file_get_contents(‘php://input’)用法

    php中的 file_get_contents('php://input')用法: file_get_contents 获取php页面中input内容的值: eg: php: 页面提交了usernam ...

  3. file_get_contents高級用法

    首先解決file_get_contents的超時問題,在超時返回錯誤後就象js中的settimeout那樣進行一次嘗試,錯誤超過3次或者5次後就確認為無法連線伺服器而徹底放棄.這裡就簡單介紹兩種解決方 ...

  4. file_get_contents("php://input")的用法

    $data = file_get_contents("php://input"); php://input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好使用 ...

  5. file_put_contents以及file_get_contents的用法与在使用过程中遇到的问题(PHP学习)

    对数据的操作最基本的是增删改查,file_put_contents以及file_get_contents是对文件里的数据进行存入与取出. 先上代码: <?php $str = 'hello wo ...

  6. file_get_contents()/file_put_contents()

    PHP file_get_contents() 函数 定义和用法 file_get_contents() 把整个文件读入一个字符串中. 该函数是用于把文件的内容读入到一个字符串中的首选方法.如果服务器 ...

  7. 解决file_get_contents遇到中文文件名无法打开问题

    利用file_get_contents打开文件或采集远程服务器文件如果文名或url中碰到汉字中文那么会出现failed to open stream:Lnvalid argument in错误.   ...

  8. php中的curl使用入门教程和常见用法实例

    摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...

  9. PHP中header用法详解带范例(转)

    header的用法 header()函数的作用是:发送一个原始 HTTP 标头[Http Header]到客户端.标头 (header) 是服务器以 HTTP 协义传 HTML 资料到浏览器前所送出的 ...

随机推荐

  1. 20161011001 treeView 递归

    protected void FillTree()        {            H_data H_data = new H_data(); H_data.sql_text1 = " ...

  2. AChecker + Selenium2对需要登录的页面进行自动化可访问性测试

    前言:这段时间还算比较空闲,我准备把过去做过的有些形形色色,甚至有些奇怪的研究总结一下,也许刚好有人用的着也不一定,不枉为之抓耳挠腮的时光和浪费的电力.   名词解释: 网站可访问性测试:国内基本没有 ...

  3. 【Spring】简单的Spring AOP注解示例

    引入相关包: <properties> <spring.version>3.0.5.RELEASE</spring.version> <aspectj.ver ...

  4. T检验与F检验的区别_f检验和t检验的关系

    1,T检验和F检验的由来 一般而言,为了确定从样本(sample)统计结果推论至总体时所犯错的概率,我们会利用统计学家所开发的一些统计方法,进行统计检定. 通过把所得到的统计检定值,与统计学家建立了一 ...

  5. react native改变app的图标和名称

    beauty\android\app\src\main\res

  6. GOLANG 基本数据类型 浮点型

    浮点型 主要为了表示小数 也可细分float32和float64两种 float64提供比float32更高的精度 取值范围 类型 最大值 最小非负数 float32 3.40282346638528 ...

  7. HTML5滑动(swipe)事件

    移动H5开发中经常用到滑动效果(页面上移.下移.向左滑动.向右滑动等),浏览器并没有内置swipe事件,可以通过touch事件(touchstart.touchmove和touchend)模拟swip ...

  8. Ubuntu install g++

    We can use two ways to  install g++ on Ubuntu. 1.  a. sudo apt-get install make gcc g++.      b. sud ...

  9. C#返回时间格式转换成 js 字符串

    在.net 中,调用 post 或者 get和后台通信时,如果有时间返回信息,后台返回的时间信息一般是这样格式:Thu Jul 9 23:14:53 UTC+0800 2015,那么要在前台显示就会有 ...

  10. maven 使用tomcat插件 自动化部署war

    1.相关环境变量 idea tomcat8 maven3 2.增加tomcat user, 修改 $CATALINA_HOME/conf/tomcat-users.xml <tomcat-use ...