当前位置:首页 > PHP教程 > php函数 > 列表

php 读取文件函数

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-19 22:36:23 浏览: 评论: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.phpfensi.com);  
  3. print_r($http_response_header);   
  4. // or  
  5. $fp = fopen('http://www.example.com''r');  
  6. print_r(stream_get_meta_data($fp));  
  7. fclose($fp);  
  8. ?> 

示例代码2,代码如下:

  1. <?php  
  2. $data = array ('foo' => 'bar');  
  3. $data = http_build_query($data);  
  4. $opts = array (  
  5.     'http' => array (  
  6.         'method' => 'post',  
  7.         'header'=> "content-type: application/x-www-form-urlencoded " .  
  8.                    "content-length: " . strlen($data) . " ",  
  9.         'content' => $data  
  10.     ),  
  11. );  
  12. $context = stream_context_create($opts);  
  13. $html = file_get_contents('http://www.phpfensi.com', false, $context);  
  14. echo $html;  
  15. ?> 

实例三

获取过来以后自动输出到浏览器,我们有没有其他的方式组织获取的信息,然后控制其输出的内容呢?完全没有问题,在curl_setopt()函数的参数中,如果希望获得内容但不输出,使用curlopt_returntransfer 参数,并设为非0值/true!,完整代码请看:

  1. <?php// create a new curl resource  $ch = curl_init();  // set url and other appropriate options  curl_setopt($ch, curlopt_url, “http://www.google.nl/”);  
  2.  curl_setopt($ch, curlopt_returntransfer, true);  // grab url, and return output  $output = curl_exec($ch);  // close curl resource, and free up system resources  curl_close($ch);  // replace ‘google’ with ‘phpit’  $output = str_replace(’google’,‘phpit’, $output);  // print output  echo $output;   
  3. //开源软件:phpfensi.com 
  4. ?>

Tags: php读取文件 php读取函数

分享到:

相关文章