PHP 文档中关于get_headers函数的介绍:
(PHP 5, PHP 7) get_headers — 取得服务器响应一个 HTTP 请求所发送的所有标头
$url = "https://www.baidu.com"; $response = get_headers($url); echo "<pre>"; print_r($response); $response = get_headers($url,1); echo '<pre>'; print_r($response);
结果:
Array ( [0] => HTTP/1.1 200 OK [1] => Server: bfe/1.0.8.18 [2] => Date: Wed, 29 Mar 2017 03:59:26 GMT [3] => Content-Type: text/html [4] => Content-Length: 14720 [5] => Connection: close [6] => Last-Modified: Tue, 14 Mar 2017 07:30:00 GMT [7] => Vary: Accept-Encoding [8] => Set-Cookie: BAIDUID=A674E52FA6AF0193F9CDEE45C7A59821:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [9] => Set-Cookie: BIDUPSID=A674E52FA6AF0193F9CDEE45C7A59821; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [10] => Set-Cookie: PSTM=1490759966; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [11] => P3P: CP=" OTI DSP COR IVA OUR IND COM " [12] => X-UA-Compatible: IE=Edge,chrome=1 [13] => Pragma: no-cache [14] => Cache-control: no-cache [15] => Accept-Ranges: bytes [16] => Set-Cookie: __bsi=18181989014312142897_00_998_N_N_8_0303_C02F_N_N_N_0; expires=Wed, 29-Mar-17 03:59:31 GMT; domain=www.baidu.com; path=/ ) Array ( [0] => HTTP/1.1 200 OK [Server] => bfe/1.0.8.18 [Date] => Wed, 29 Mar 2017 03:59:26 GMT [Content-Type] => text/html [Content-Length] => 14720 [Connection] => close [Last-Modified] => Tue, 14 Mar 2017 07:30:00 GMT [Vary] => Accept-Encoding [Set-Cookie] => Array ( [0] => BAIDUID=A674E52FA6AF0193A9330C0C2F20BED6:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [1] => BIDUPSID=A674E52FA6AF0193A9330C0C2F20BED6; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [2] => PSTM=1490759966; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [3] => __bsi=16875695485765166328_00_13_N_N_8_0303_C02F_N_N_N_0; expires=Wed, 29-Mar-17 03:59:31 GMT; domain=www.baidu.com; path=/ ) [P3P] => CP=" OTI DSP COR IVA OUR IND COM " [X-UA-Compatible] => IE=Edge,chrome=1 [Pragma] => no-cache [Cache-control] => no-cache [Accept-Ranges] => bytes )
服务器是否能够正常访问,可以使用这个函数来判断