php獲取請求url時響應的報頭信息的方法:1、取url的時候,創建名為【$http_response_header】的變量來保存http響應的報頭;2、使用fopen打開數據流信息,用【stream_get_meta_data】獲取。
php獲取請求url時響應的報頭信息的方法:
1、用file_get_contents或者fopen、file、readfile等函數讀取url的時候,會創建一個名 為$http_response_header的變量來保存http響應的報頭.
示例代碼一:
<?php $url = 'http://www.baidu.com'; $html = file_get_contents($url); print_r($http_response_header); //輸出結果 array( [0] => http/1.1 200 ok [1] => date: tue, 06 nov 2012 08:51:01 gmt [2] => server: bws/1.0 [3] => content-length: 9803 [4] => content-type: text/html;charset=gbk [5] => cache-control: private [6] => expires: tue, 06 nov 2012 08:51:01 gmt [7] => set-cookie: baiduid=6635735b51b28640f425f802c49340f2:fg=1; expires=tue, 06-nov-42 08:51:01 gmt; path=/; domain=.baidu.com [8] => p3p: cp=" oti dsp cor iva our ind com " [9] => connection: close ) ?>2、使用fopen等函數打開的數據流信息可以用 stream_get_meta_data來獲取。
示例代碼二:
<?php $fp = fopen($url, 'r'); print_r(stream_get_meta_data($fp)); fclose($fp); //輸出結果 array ( [wrapper_data] => array ( [0] => http/1.1 200 ok [1] => date: tue, 06 nov 2012 08:54:22 gmt [2] => server: bws/1.0 [3] => content-length: 9803 [4] => content-type: text/html;charset=gbk [5] => cache-control: private [6] => expires: tue, 06 nov 2012 08:54:22 gmt [7] => set-cookie: baiduid=347578bcbd709f27925bdd8b05364a73:fg=1; expires=tue, 06-nov-42 08:54:22 gmt; path=/; domain=.baidu.com [8] => p3p: cp=" oti dsp cor iva our ind com " [9] => connection: close ) [wrapper_type] => http [stream_type] => tcp_socket [mode] => r [unread_bytes] => 0 [seekable] => [uri] => http://www.baidu.com [timed_out] => [blocked] => 1 [eof] => ) ?>3、get_headers()也可以獲取請求url的響應報文。
示例代碼三:
<?php print_r(get_headers($url)); array ( [0] => http/1.1 200 ok [1] => date: tue, 06 nov 2012 08:58:41 gmt [2] => server: bws/1.0 [3] => content-length: 9803 [4] => content-type: text/html;charset=gbk [5] => cache-control: private [6] => expires: tue, 06 nov 2012 08:58:41 gmt [7] => set-cookie: baiduid=87b6f26eec74f2b8f7faba934dc6bb24:fg=1; expires=tue, 06-nov-42 08:58:41 gmt; path=/; domain=.baidu.com [8] => p3p: cp=" oti dsp cor iva our ind com " [9] => connection: close ) ?>相關學習推薦:php圖文教程
ssl有什么用承諾書在那里填-備案平臺制作網站有哪些必由之路CentOS下怎么安裝配置TinyProxy智能建站有什么好處 自助建站的步驟是怎樣的不知道是否我設置錯誤是否能協助我處理一下短信驗證超時麻煩重新提交一下備案我這邊重新操作驗證PHP放在虛擬主機的哪個文件夾?