I have previously talked about the process of using PHP to determine whether remote files exist. They all introduced the use of fopen, sockt, and curl functions to check whether remote files exist. Now I will introduce the use of get_headers to check whether remote files exist. If you need to know more Friends can refer to it.
First let’s briefly understand the get_headers() function
get_headers() returns an array containing the headers sent by the server in response to an HTTP request.
get_headers: Send server response to HTTP request
get_headers(string url[link format])
get_headers() returns the server HTTP request as an array. If execution fails, FALSE and an error level E_WARNING will be returned.
The optional parameter is set to 1, get_headers() can analyze the response speed of the system and set the keys in the array.
Note: To use this function, you need to set allow_url_fopen = On in php.ini to use it
Example
The code is as follows |
Copy code |
代码如下 |
复制代码 |
$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, 1));
?>
返回值
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
Array
(
[0] => HTTP/1.1 200 OK
[Date] => Sat, 29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
[ETag] => "3f80f-1b6-3e1cb03b"
[Accept-Ranges] => bytes
[Content-Length] => 438
[Connection] => close
[Content-Type] => text/html
)
|
$url = 'http://www.example.com';
print_r(get_headers($url));
代码如下 |
复制代码 |
//判断远程文件是否存在
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;
}
|
print_r(get_headers($url, 1));<🎜>
?>
Return value
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
Array
(
[0] => HTTP/1.1 200 OK
[Date] => Sat, 29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
[ETag] => "3f80f-1b6-3e1cb03b"
[Accept-Ranges] => bytes
[Content-Length] => 438
[Connection] => close
[Content-Type] => text/html
)
|
Example
The code is as follows |
Copy code |
//Determine whether the remote file exists
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;
} <🎜>
|
例2
排除重定向的例子:
代码如下 |
复制代码 |
/**
代码如下 |
复制代码 |
/**
* 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;
} |
* Fetches all the real headers sent by the server in response to a HTTP request without redirects
* Get headers without redirection
*/
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;
} |
http://www.bkjia.com/PHPjc/445281.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445281.htmlTechArticle以前我有讲过程关于php判断远程文件是否存在的文章,那里都介绍利用fopen,sockt,curl函数来实现检查远程文件是否存在了,下面我再介绍利用...