PHP利用get_headers()函数判断远程的url地址是否有效

时间:2023-03-08 16:04:43
PHP利用get_headers()函数判断远程的url地址是否有效

问题:

利用url访问远程的文件、图片、视频时有时需要请求前判断url地址是否有效。

解决办法:

(PHP 5, PHP 7)

get_headers — 取得服务器响应一个 HTTP 请求所发送的所有标头。

利用PHP自带的函数get_headers(),利用http返回值是否存在200状态,来判断url地址是否有效。

get_headers()函数官方介绍:http://php.net/manual/zh/function.get-headers.php

具体实现代码如下:

案例一:

$url = "https://www.baidu.com";
$response = get_headers($url);
echo "<pre>";
var_dump($response);
$response = get_headers($url,1);//如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。
echo '<pre>';
var_dump($response);

打印结果如下:

array(16) {
[0]=>string(15) "HTTP/1.0 200 OK"
[1]=>string(20) "Accept-Ranges: bytes"
[2]=>string(23) "Cache-Control: no-cache"
[3]=>string(21) "Content-Length: 14722"
[4]=>string(23) "Content-Type: text/html"
[5]=>string(35) "Date: Wed, 20 Feb 2019 13:12:31 GMT"
[6]=>string(21) "Etag: "5c653bc8-3982""
[7]=>string(44) "Last-Modified: Thu, 14 Feb 2019 09:58:32 GMT"
[8]=>string(39) "P3p: CP=" OTI DSP COR IVA OUR IND COM ""
[9]=>string(16) "Pragma: no-cache"
[10]=>string(15) "Server: BWS/1.1"
[11]=>string(141) "Set-Cookie: BAIDUID=72E4B8623F9E998C790B22F8E8D64BEC:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"
[12]=>string(137) "Set-Cookie: BIDUPSID=72E4B8623F9E998C790B22F8E8D64BEC; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"
[13]=>string(111) "Set-Cookie: PSTM=1550668351; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"
[14]=>string(21) "Vary: Accept-Encoding"
[15]=>string(33) "X-Ua-Compatible: IE=Edge,chrome=1"
}
array(14) {
[0]=>string(15) "HTTP/1.0 200 OK"
["Accept-Ranges"]=>string(5) "bytes"
["Cache-Control"]=>string(8) "no-cache"
["Content-Length"]=>string(5) "14722"
["Content-Type"]=>string(9) "text/html"
["Date"]=>string(29) "Wed, 20 Feb 2019 13:12:31 GMT"
["Etag"]=>string(15) ""5c653bc8-3982""
["Last-Modified"]=>string(29) "Thu, 14 Feb 2019 09:58:32 GMT"
["P3p"]=>string(34) "CP=" OTI DSP COR IVA OUR IND COM ""
["Pragma"]=>string(8) "no-cache"
["Server"]=>string(7) "BWS/1.1"
["Set-Cookie"]=>array(3) {
[0]=>string(129) "BAIDUID=72E4B8623F9E998CF68FDDAD465EAF4A:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"
[1]=>string(125) "BIDUPSID=72E4B8623F9E998CF68FDDAD465EAF4A; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"
[2]=>string(99) "PSTM=1550668351; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"
}
["Vary"]=>string(15) "Accept-Encoding"
["X-Ua-Compatible"]=>string(16) "IE=Edge,chrome=1"
}

案例二:

$url = "https://www.baidu.com";
$response = get_headers($url);
if(preg_match('/200/',$response[0])){
echo "<pre/>";
var_dump($response[0]);
}else{
var_dump("无效url资源!");
}

打印结果如下:

string(15) "HTTP/1.0 200 OK"

注意点:如果提示错误,需要在php.ini开启:allow_url_fopen=on

遇到get_headers()请求https报错解决思路

场景:使用get_headers()去校验该https类型的url是否能正确响应时

结果报错,如下:

get_headers(): SSL operation failed with code 1.
OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

具体原因:
get_headers()会对url发出请求HTTP请求,获取服务器响应头信息,遇到url为https时,会去校验签名证书

解决思路:
关闭证书校验

具体实现代码如下:

$url = "https://www.baidu.com";

//关闭https证书校验
stream_context_set_default( [
'ssl' => [
'verify_host' => false,
'verify_peer' => false,
'verify_peer_name' => false,
],
]); $response = get_headers($url);
if(preg_match('/200/',$response[0])){
echo "<pre/>";
var_dump($response[0]);
}else{
var_dump("无效url资源!");
}