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

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.example.com/');
print_r($http_response_header);
$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 (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
 "Content-Length: " . strlen($data) . "\r\n",
 'content' => $data
),
);

$context = stream_context_create($opts);
$html = file_get_contents('http://www.example.com', false, $context);
echo $html;
?>
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

已有 2 条评论 »

  1. 大吃白菜 大吃白菜

    我就要来吃白菜,谁也挡不住。

  2. 大吃白菜 大吃白菜

    博主很给力。

白菜的弟弟的同学的老师的儿子的妈妈养的小狗的表弟的主人的朋友说看帖不回会被鄙视de

添加新评论 »

【f(x,y)=(y^2-4y)(x^2-6x)的极值(请填入答案,答案见本表单title)】