1、用file_get_contents或者fopen、file、readfile等函數(shù)讀取url的時候,會創(chuàng)建一個名為$http_response_header的變量來保存http響應(yīng)的報頭,使用fopen等函數(shù)打開的數(shù)據(jù)流信息可以用stream_get_meta_data來獲取.
2、php5中新增的參數(shù)context使這些函數(shù)更加靈活,通過它我們可以定制http請求,甚至post數(shù)據(jù).
示例代碼1,如下:
- <?php
- $html = file_get_contents('http://www.companysz.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 " .
- "content-length: " . strlen($data) . " ",
- 'content' => $data
- ),
- );
- $context = stream_context_create($opts);
- $html = file_get_contents('http://www.companysz.com', false, $context);
- echo $html;
- ?>
實例三
獲取過來以后自動輸出到瀏覽器,我們有沒有其他的方式組織獲取的信息,然后控制其輸出的內(nèi)容呢?完全沒有問題,在curl_setopt()函數(shù)的參數(shù)中,如果希望獲得內(nèi)容但不輸出,使用curlopt_returntransfer 參數(shù),并設(shè)為非0值/true!,完整代碼請看:
- <?php?// create a new curl resource $ch = curl_init(); // set url and other appropriate options curl_setopt($ch, curlopt_url, “http://www.google.nl/”);
- curl_setopt($ch, curlopt_returntransfer, true); // grab url, and return output $output = curl_exec($ch); // close curl resource, and free up system resources curl_close($ch); // replace ‘google’ with ‘phpit’ $output = str_replace(’google’,‘phpit’, $output); // print output echo $output;
- //開源軟件:Vevb.com
- ?>
新聞熱點
疑難解答