Php file reading function. 1. when you use file_get_contents or fopen, file, readfile, and other functions to read a url, a variable named $ http_response_header is created to save the http response header, when you use fopen and other functions such as file_get_contents or fopen, file, and readfile to read a url, a variable named $ http_response_header is created to save the http response header, stream_get_meta_data can be used to obtain data streams opened by functions such as fopen. 2. the context parameter added in php5 makes these functions more flexible. through it, we can customize http requests or even post data.
1. when you use file_get_contents or fopen, file, readfile, and other functions to read a url, a variable named $ http_response_header is created to save the http response header, stream_get_meta_data can be used to obtain data streams opened by functions such as fopen.
2. the context parameter added in php Tutorial 5 makes these functions more flexible. through this function, we can customize http requests or 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;
?>
Instance 3
After the information is obtained, it is automatically output to the browser. Is there any other way to organize the obtained information and control the output content? No problem at all. in the curl_setopt () function parameter, if you want to obtain the content but do not output it, use the curlopt_returntransfer parameter and set it to a non-0 value/true !, Complete code can be found:
...