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. DP专题训练之HDU 1231 最大连续子序列

    Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...

  2. 【转】创建SVN仓库的步骤

    转载地址:http://www.cnblogs.com/ivan0626/p/3783053.html   今天在客户现场联调,两个开发人员之间的代码想用SVN来管理,所以就临时在本地机器上搭建一个S ...

  3. 【转】如何配置android的adb环境变量

    转载地址:http://jingyan.baidu.com/article/17bd8e52f514d985ab2bb800.html 对于android的开发人员来说,首先要做的就是环境变量的配置. ...

  4. javascript数据属性和访问器属性

    var book={ _year:2004, edition:1};Object.defineProperty(book,"year",{ get:function(){ retu ...

  5. js跨域访问,No 'Access-Control-Allow-Origin' header is present on the requested resource

    js跨域访问提示错误:XMLHttpRequest cannot load http://...... No 'Access-Control-Allow-Origin' header is prese ...

  6. 安装软件时出现error 1337 【添加权限】

    Error 1317 An error occurred while attempting to create the directory Drive Name:\Folder Name https: ...

  7. 数据库:django ORM如何处理N+1查询

    数据库N+1查询是个常见的问题,简单描述场景如下 基本场景 class Category(models.Model): name = models.CharField(max_length=30) c ...

  8. PDO操作数据库

  9. 增量处理属性之记录模式(Record Modes)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. 空间点绕轴旋转公式&程序(C++)

    关键词:空间旋转.旋转轴 用途:相机位姿估计.无人机位姿估计.3D游戏.3D建模 文章类型:概念.公式总结(本文不带推倒过程,若想了解公式是如何推出来的请搜索文献),C++函数展示 @Author:V ...