PHP使用get_headers函數(shù)判斷遠程文件是否存在的方法
本文實例講述了PHP使用get_headers函數(shù)判斷遠程文件是否存在的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
以前講過程關(guān)于php判斷遠程文件是否存在的文章都是利用fopen,sockt,curl函數(shù)來實現(xiàn)檢查遠程文件是否存在,下面我再介紹利用 get_headers來檢查遠程文件是否存在,感興趣的朋友可以參考一下。
先來簡單了解get_headers()函數(shù)
get_headers() 返回一個數(shù)組m包含有服務(wù)器響應(yīng)一個 HTTP 請求所發(fā)送的標頭。
get_headers:發(fā)送服務(wù)器響應(yīng)HTTP請求
get_headers(字符串url[鏈接格式])
get_headers()以數(shù)組的形式返回服務(wù)器HTTP請求m如果執(zhí)行失敗,將返回FALSE和一個錯誤的水平E_WARNING,
可選參數(shù)設(shè)置為1,get_headers()能分析系統(tǒng)的響應(yīng)速度和集數(shù)組中的鍵,
注意:使用該函數(shù)需要把 php.ini里面的allow_url_fopen = On,才能使用
實例代碼如下:
$url = 'http://www.dbjr.com.cn';
print_r(get_headers($url));
print_r(get_headers($url, 1));
?>
運行結(jié)果如下:
(
[0] => HTTP/1.1 200 OK
[1] => Cache-Control: max-age=1800
[2] => Content-Length: 54874
[3] => Content-Type: text/html
[4] => Content-Location: http://www.dbjr.com.cn/index.htm
[5] => Last-Modified: Fri, 28 Nov 2014 03:34:56 GMT
[6] => Accept-Ranges: bytes
[7] => ETag: "b66ba847bcad01:bc5"
[8] => Server: Microsoft-IIS/6.0
[9] => Date: Fri, 28 Nov 2014 03:37:34 GMT
[10] => Connection: close
)
Array
(
[0] => HTTP/1.1 200 OK
[Cache-Control] => max-age=1800
[Content-Length] => 54874
[Content-Type] => text/html
[Content-Location] => http://www.dbjr.com.cn/index.htm
[Last-Modified] => Fri, 28 Nov 2014 03:34:56 GMT
[Accept-Ranges] => bytes
[ETag] => "b66ba847bcad01:bc5"
[Server] => Microsoft-IIS/6.0
[Date] => Fri, 28 Nov 2014 03:37:35 GMT
[Connection] => close
)
判斷遠程文件是否存在代碼如下:
function remote_file_exists($url) {
$executeTime = ini_get('max_execution_time');
ini_set('max_execution_time', 0);
$headers = @get_headers($url);
ini_set('max_execution_time', $executeTime);
if ($headers) {
$head = explode(' ', $headers[0]);
if ( !emptyempty($head[1]) && intval($head[1]) < 400) return true;
}
return false;
}
排除重定向的實例代碼如下:
/**
* Fetches all the real headers sent by the server in response to a HTTP request without redirects
* 獲取不包含重定向的報頭
*/
function get_real_headers($url,$format=0,$follow_redirect=0) {
if (!$follow_redirect) {
//set new default options
$opts = array('http' =>
array('max_redirects'=>1,'ignore_errors'=>1)
);
stream_context_get_default($opts);
}
//get headers
$headers=get_headers($url,$format);
//restore default options
if (isset($opts)) {
$opts = array('http' =>
array('max_redirects'=>20,'ignore_errors'=>0)
);
stream_context_get_default($opts);
}
//return
return $headers;
}
?>
希望本文所述對大家的PHP程序設(shè)計有所幫助。
- php中檢查文件或目錄是否存在的代碼小結(jié)
- php file_exists 檢查文件或目錄是否存在的函數(shù)
- PHP中判斷文件存在使用is_file還是file_exists?
- PHP判斷文件是否存在、是否可讀、目錄是否存在的代碼
- php判斷文件夾是否存在不存在則創(chuàng)建
- php下利用curl判斷遠程文件是否存在的實現(xiàn)代碼
- PHP判斷網(wǎng)絡(luò)文件是否存在的方法
- PHP判斷遠程圖片或文件是否存在的實現(xiàn)代碼
- php中判斷文件存在是用file_exists還是is_file的整理
- PHP讀取文件內(nèi)容的五種方式
- php讀取文件內(nèi)容到數(shù)組的方法
- PHP檢查文件是否存在,不存在自動創(chuàng)建及讀取文件內(nèi)容操作示例
相關(guān)文章
PHP面向?qū)ο蟪绦蛟O(shè)計之構(gòu)造方法和析構(gòu)方法詳解
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計之構(gòu)造方法和析構(gòu)方法,結(jié)合具體實例形式詳細分析了php面向?qū)ο蟪绦蛟O(shè)計中構(gòu)造方法和析構(gòu)方法的相關(guān)概念、原理、用法及操作注意事項,需要的朋友可以參考下2019-06-06dede3.1分頁文字采集過濾規(guī)則詳說(圖文教程)續(xù)四
dede3.1分頁文字采集過濾規(guī)則詳說(圖文教程)續(xù)四...2007-04-04