search
Homephp教程php手册php 读取文件函数

php 读取文件函数

Jun 13, 2016 am 11:18 AM
filefopengetphpreadfilefunctiondocumentuseread

 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!,完整代码请看:


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor