Bookmark and Share

Lee's 程序人生

HTML CSS Javascript XML AJAX ATLAS C# C++ 数据结构 软件工程 设计模式 asp.net Java 数字图象处理 Sql 数据库
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

php中几个文件读取函数的贴心功能

Posted on 2008-06-13 10:09  analyzer  阅读(401)  评论(0)    收藏  举报

1、用file_get_contents或者fopen、file、readfile等函数读取url的时候,会创建一个名为$http_response_header的变量来保存http响应的报头,使用fopen等函数打开的数据流信息可以用stream_get_meta_data来获取。
2、php5中新增的参数context使这些函数更加灵活,通过它我们可以定制http请求,甚至post数据。

示例代码1:


 1<?php     
 2$html = file_get_contents('http://www.example.com/');     
 3print_r($http_response_header);     
 4      
 5// or     
 6$fp = fopen('http://www.example.com/', 'r');     
 7print_r(stream_get_meta_data($fp));     
 8fclose($fp);     
 9?>   
10

示例代码2:
 1<?php     
 2$data = array ('foo' => 'bar');     
 3$data = http_build_query($data);     
 4      
 5$opts = array (     
 6    'http' => array (     
 7        'method' => 'POST',     
 8        'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .     
 9                   "Content-Length: " . strlen($data. "\r\n",     
10        'content' => $data     
11    ),     
12);     
13      
14$context = stream_context_create($opts);     
15$html = file_get_contents('http://www.example.com', false, $context);     
16      
17echo $html;     
18?>    
19

参考:
http://cn.php.net/manual/zh/function.file-get-contents.php
http://cn.php.net/manual/en/function.stream-context-create.php
http://cn.php.net/manual/zh/wrappers.http.php
来源:http://www.ugia.cn/?p=140