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,如下:

  1. <?php  
  2. $html = file_get_contents('http://www.phpfensi.com);  
  3. print_r($http_response_header);   
  4. // or  
  5. $fp = fopen('http://www.example.com''r');  
  6. print_r(stream_get_meta_data($fp));  
  7. fclose($fp);  
  8. ?> 

示例代碼2,代碼如下:

  1. <?php  
  2. $data = array ('foo' => 'bar');  
  3. $data = http_build_query($data);  
  4. $opts = array (  
  5.     'http' => array (  
  6.         'method' => 'post',  
  7.         'header'=> "content-type: application/x-www-form-urlencoded " .  
  8.                    "content-length: " . strlen($data) . " ",  
  9.         'content' => $data  
  10.     ),  
  11. );  
  12. $context = stream_context_create($opts);  
  13. $html = file_get_contents('http://www.phpfensi.com', false, $context);  
  14. echo $html;  
  15. ?> 

實例三

獲取過來以后自動輸出到瀏覽器,我們有沒有其他的方式組織獲取的信息,然后控制其輸出的內(nèi)容呢?完全沒有問題,在curl_setopt()函數(shù)的參數(shù)中,如果希望獲得內(nèi)容但不輸出,使用curlopt_returntransfer 參數(shù),并設(shè)為非0值/true!,完整代碼請看:

  1. <?php?// create a new curl resource  $ch = curl_init();  // set url and other appropriate options  curl_setopt($ch, curlopt_url, “http://www.google.nl/”);  
  2.  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;   
  3. //開源軟件:phpfensi.com 
  4. ?>
轉(zhuǎn)載請注明來源:php 讀取文件函數(shù)

  哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網(wǎng)站建設(shè)。