Home > Backend Development > PHP Tutorial > php read file function_PHP tutorial

php read file function_PHP tutorial

WBOY
Release: 2016-07-20 11:01:54
Original
994 people have browsed it

1. When using file_get_contents or fopen, file, readfile and other functions to read the url, a variable named $http_response_header will be created to save the header of the http response. The data stream information opened using functions such as fopen can be obtained with stream_get_meta_data. 2. The new parameter context in php5 makes these functions more flexible. Through it, we can customize http requests and even post data. ​

1. When using file_get_contents or fopen, file, readfile and other functions to read the url, a variable named $http_response_header will be created to save the header of the http response. The data stream information opened using functions such as fopen can be obtained using stream_get_meta_data Get.

2. The new parameter context in PHP tutorial 5 makes these functions more flexible. Through it, we can customize http requests and even post data.

Sample code 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);
?>

Sample code 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;
?>

Example 3

After obtaining it, it will be automatically output to the browser. Is there any other way for us to organize the obtained information and then control its output content? There is no problem at all. In the parameters of the curl_setopt() function, if you want to obtain the content but do not For output, use the curlopt_returntransfer parameter and set it to a non-zero value/true!. For the complete code, please see:


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445414.htmlTechArticle1. When using file_get_contents or fopen, file, readfile and other functions to read the url, a file named $ will be created. The variable http_response_header is used to save the header of the http response, using functions such as fopen...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template