get_headers()是PHP中一个系统级函数,他可以返回一个包括有处事器响应一个HTTP请求所发送的标头的数组。假如失败则返回 FALSE 并发出一条 E_WARNING 级此外错误信息(可用来判定长途文件是否存在)。
PHP get_headers()函数的界说与语法get_headers():可以通过处事器的响应头来判定长途文件是否存在
语法:
array get_headers ( string $url [, int $format = 0 ] )参数:
format 假如将可选的 format 参数设为 1,则 get_headers() 会理会相应的信息并设定命组的键名,假如配置默认为0
get_headers()举例
PHP判定一个网站是否可以会见例1:
<?php var_dump( get_headers('https://www.feiniaomy.com')); ?>返回功效:
Array ( [0] => HTTP/1.1 200 OK [1] => Server: Tengine [2] => Content-Type: text/html; charset=utf-8 [3] => Connection: close [4] => Vary: Accept-Encoding [5] => Date: Wed, 15 May 2019 03:00:39 GMT [6] => Vary: Accept-Encoding [7] => X-Powered-By: PHP/7.2.11 [8] => Product: Z-BlogPHP 1.5.2 Zero [9] => X-XSS-Protection: 1; mode=block [10] => Upgrade-Insecure-Requests: 1 [11] => Ali-Swift-Global-Savetime: 1557889239 [12] => Via: cache4.l2eu6-1[68,200-0,M], cache28.l2eu6-1[70,0], kunlun9.cn547[153,200-0,M], kunlun10.cn547[157,0] [13] => X-Cache: MISS TCP_MISS dirn:-2:-2 [14] => X-Swift-SaveTime: Wed, 15 May 2019 03:00:39 GMT [15] => X-Swift-CacheTime: 0 [16] => Timing-Allow-Origin: * [17] => EagleId: db939d1e15578892393137219e )留意:以上示例get_headers()函数没有配置参数,所以返回的数组中键值都是默认的。
例2:
<?php print_r( get_headers('https://www.feiniaomy.com',1)); ?>返回功效:
Array ( [0] => HTTP/1.1 200 OK [Server] => Tengine [Content-Type] => text/html; charset=utf-8 [Connection] => close [Vary] => Array ( [0] => Accept-Encoding [1] => Accept-Encoding ) [Date] => Wed, 15 May 2019 03:03:16 GMT [X-Powered-By] => PHP/7.2.11 [Product] => Z-BlogPHP 1.5.2 Zero [X-XSS-Protection] => 1; mode=block [Upgrade-Insecure-Requests] => 1 [Ali-Swift-Global-Savetime] => 1557889396 [Via] => cache4.l2eu6-1[81,200-0,M], cache24.l2eu6-1[83,0], kunlun9.cn547[124,200-0,M], kunlun7.cn547[128,0] [X-Cache] => MISS TCP_MISS dirn:-2:-2 [X-Swift-SaveTime] => Wed, 15 May 2019 03:03:16 GMT [X-Swift-CacheTime] => 0 [Timing-Allow-Origin] => * [EagleId] => db939d1b15578893967421125e )PS:本示例挪用get_headers()函数时,配置了他的参数为1,返回的功效数组中,会理会相应的信息并设定命组的键名。
PHP判定长途图片是否存在通过以上的示例的返回功效,我们可以通过返回数组中的第一个值来判定一个长途的页面或长途的图片是否可以会见
代码:
<?php $arr = get_headers('https://www.feiniaomy.com/zb_users/upload/Mochu_Feiniao/logo.png', 1); if(!(stristr($arr[0], "200") && stristr($arr[0], "OK"))) { echo '图片不存在'; }else{ echo '图片存在'; } ?> 执行功效:图片存在上面的示例是判定一个长途的图片是否可以会见,虽然你也可以把图片的地点换成网站的url地点,同样可以做出判定