php 读取文件函数_PHP教程

原创
2016-07-20 11:01:54 698浏览

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

 1、用file_get_contents或者fopen、file、readfile等函数读取url的时候,会创建一个名为$http_response_header的变量来保存http响应的报头,使用fopen等函数打开的数据流信息可以用stream_get_meta_data来获取。

  2、php教程5中新增的参数context使这些函数更加灵活,通过它我们可以定制http请求,甚至post数据。

  示例代码1:

$html = file_get_contents('http://www.bkjia.com);
print_r($http_response_header);  
// or
$fp = fopen('http://www.example.com', 'r');
print_r(stream_get_meta_data($fp));
fclose($fp);
?>

  示例代码2:

$data = array ('foo' => 'bar');
$data = http_build_query($data);
$opts = array (
'http' => array (
'method' => 'post',
'header'=> "content-type: application/x-www-form-urlencodedrn" .
"content-length: " . strlen($data) . "rn",
'content' => $data
),
);
$context = stream_context_create($opts);
$html = file_get_contents('http://www.example.com', false, $context);
echo $html;
?>

实例三

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445414.htmlTechArticle1、用file_get_contents或者fopen、file、readfile等函数读取url的时候,会创建一个名为$http_response_header的变量来保存http响应的报头,使用fopen等函...
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。