file_get_contents的深入
来源:互联网 发布:2016淘宝分销刷信誉 编辑:程序博客网 时间:2024/05/18 03:56
file_get_contents()用法:
string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )
参数详解:
第一个参数:文件路径或者远程主机资源的URL
第二个参数:是否使用php.ini中定义的include_path,如果设为true,将去php.ini中定义的include_path中寻找
第三个参数:资源流上下文(十分重要),用stream_content_create()创建
第四个参数:起始位置
第五个参数:长度
用处:
1. 获取本地文件内容
将一个文件内容读入一个字符串中,一般是将文件里的全部内容读入内存中,所以比较大的文件一般要用这个函数
$str = file_get_contents(01.txt);//将01.txt的内容写入$strecho $str;
2.用获取某个url的内容
$str = file_get_content('http://www.baidu.com');//获取www.baidu.com页面的内容echo $str;
3.利用file_get_content模拟http的GET和POST请求
/*@function:用file_get_contents发送GET请求*/$url = 'http://localhost/01.php';//定义请求URL$method = 'GET';//定义请求方法$header = 'Cookie: name=testname;';//定义请求头信息$options = array( 'http' => array( 'method' => $method, 'header' => $header, ) );$content = stream_context_create($options);//生成资源上下文echo file_get_contents($url,false,$content);
注意:设置COOKIE是应该COOKIE的字符串拼接在header最后,以免对其他的header信息造成影响
/*@function:用file_get_contents发送POST请求*/$url = 'http://localhost/inertview/01.php';//定义请求URL$method = 'POST';//定义请求方法$content = array( 'username' => 'root', 'password' => '123456' );$content = http_build_query($content);//定义请求的主体信息$header = "Content-Type: application/x-www-form-urlencoded\r\n"."Content-Length: ".strlen($content)."\r\n".'Cookie: name=testname;';//定义请求头信息$options = array( 'http' => array( 'method' => $method, 'header' => $header, 'content' => $content ) );$content = stream_context_create($options);echo file_get_contents($url,false,$content);//获取内容
0 0
- file_get_contents的深入
- file_get_contents的post请求
- file_get_contents 的使用
- file_get_contents
- file_get_contents
- file_get_contents()
- php增强file_get_contents的兼容性
- file_get_contents与curl的比较
- fopen,file_get_contents,curl的区别
- PHP:file_get_contents的代理使用方法
- php 使用file_get_contents的问题
- curl与file_get_contents的比较
- file_get_contents与curl的比较
- file_get_contents("php://input")的使用方法
- file_get_contents("php://input")的使用方法
- PHP file_put_contents&file_get_contents的使用
- file_get_contents("php://input")的使用方法
- file_get_contents("php://input")的使用方法
- 高效位运算 __builtin_系列函数
- 我火狐浏览器的好多TMP的临时文件都跑桌面来了,怎么修改存放地方啊?
- 编译错误:undefined reference to `clock_gettime'
- 不相交集合的数据结构
- C++函数可变参数实现原理探究——以实现printf为例
- file_get_contents的深入
- BZOJ1257 [CQOI2007]余数之和sum(枚举商)
- Matlab图像处理系列3———空间域锐化滤波器
- 卫星照片
- C++中如何计算程序运行的时间
- HDU 1317 Bellman-Ford
- leetcode--Intersection of Two Linked Lists
- reTextarea("content")函数---FCKEditor编辑器使用例子
- 两个工程关联应注意的问题