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: