加入收藏 | 设为首页 | 会员中心 | 我要投稿 上饶站长网 (https://www.0793zz.com.cn/)- 数据库平台、视觉智能、智能搜索、决策智能、迁移!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php 读取文件函数

发布时间:2022-07-29 10:44:36 所属栏目:PHP教程 来源:互联网
导读:1、用file_get_contents或者fopen、file、readfile等函数读取url的时候,会创建一个名为$http_response_header的变量来保存http响应的报头,使用fopen等函数打开的数据流信息可以用stream_get_meta_data来获取. 2、php5中新增的参数context使这些函数更加灵活,
  1、用file_get_contents或者fopen、file、readfile等函数读取url的时候,会创建一个名为$http_response_header的变量来保存http响应的报头,使用fopen等函数打开的数据流信息可以用stream_get_meta_data来获取.
 
  2、php5中新增的参数context使这些函数更加灵活,通过它我们可以定制http请求,甚至post数据.
 
  示例代码1,如下:
 
  <?php  
  $html = file_get_contents('http://www.phpfensi.com);  
  print_r($http_response_header);  
  // or  
  $fp = fopen('http://www.example.com', 'r');  
  print_r(stream_get_meta_data($fp));  
  fclose($fp);  
  ?>
  示例代码2,代码如下:
 
  <?php  
  $data = array ('foo' => 'bar');  
  $data = http_build_query($data);  
  $opts = array (  
      'http' => array (  
          'method' => 'post',  
          'header'=> "content-type: application/x-www-form-urlencoded " .  
                     "content-length: " . strlen($data) . " ",  
          'content' => $data  
      ),  
  );  
  $context = stream_context_create($opts);  
  $html = file_get_contents('http://www.phpfensi.com', false, $context);  
  echo $html;  
  ?>
  实例三
 
  获取过来以后自动输出到浏览器,我们有没有其他的方式组织获取的信息,然后控制其输出的内容呢?完全没有问题,在curl_setopt()函数的参数中,如果希望获得内容但不输出,使用curlopt_returntransfer 参数,并设为非0值/true!,完整代码请看:
 
  <?php​// create a new curl resource  $ch = curl_init();  // set url and other appropriate options  curl_setopt($ch, curlopt_url, “http://www.google.nl/”);  
   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;   
  //开源软件:phpfensi.com
  ?>

(编辑:上饶站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读