PHP为fopen,file_get_contents等函数请求web地址时增加Http头的方法
2014-01-19 16:15
686 查看
我们在使用fsockopen时可以方便的自定义自己请求的http头内容来访问某些对客户端请求头有特殊限制的网站,但是使用fopen,file_get_contents等函数请求web地址时怎么来灵活定义请求的http头呢? 解决方案:stream_context_create() 函数 fopen实现 <?php $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-language: en\r\n" . "Cookie: foo=bar\r\n" ) ); $context = stream_context_create($opts); $fp = fopen('http://www.example.com', 'r', false, $context); fpassthru($fp); fclose($fp); ?> file_get_contents实现 <?php $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-language: en\r\n" . "Cookie: foo=bar\r\n" ) ); $context = stream_context_create($opts); file_get_contents('http://www.example.com', null, $context); ?>
http://be-evil.org/post-174.html
相关文章推荐
- PHP使用file_get_contents或curl请求https的域名内容为空或Http 505错误的问题排查方法
- 深入php函数file_get_contents超时处理的方法详解
- php读取本地文件常用函数(fopen与file_get_contents)
- php 在linux 用fopen() 函数打开,file_get_contents(),fread()函数 读取 另外一台服务器映射过来的文件 总是返回false,null的情况。
- 深入php函数file_get_contents超时处理的方法详解
- PHP中几种HTTP请求的实现方法及比较: file_get_contents vs. cURL vs. PECL_HTTP
- php读取网络文件curl,fsockopen,file_get_contents,file,fopen几种方法
- file_get_contents无法请求https连接的解决方法 php开启curl
- php读取本地文件常用函数(fopen与file_get_contents)
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- ThinPHP第二十八天(F函数和file_put_contents区别|PHP生成PHP文件,Kindeditor编辑器使用方法)
- file_get_contents("php://input")的使用方法
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- PHP中fopen,file_get_contents,curl函数的区别和性能
- [PHP]fopen、file_get_contents、curl区别
- PHP http(file_get_content) GET与POST请求方式
- php发送Http请求,抓取网页数据方法(cURL,file_get_contents,snoopy)
- PHP fopen()和 file_get_contents() 应用与差异。
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- 关于PHP程序使用file_get_content()函数进行抓取PHP程序与smarty结合编译过程中产生的静态文件,抓取不了?连接超时?(地址映射)