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);
-
- // or
- $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 (
- 'http' => 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