Heim  >  Artikel  >  Backend-Entwicklung  >  php get_headers 判断URL是否有效的方法介绍

php get_headers 判断URL是否有效的方法介绍

WBOY
WBOYOriginal
2016-07-25 08:59:46981Durchsuche
php get_headers可以用来判断URL是否有效,本文为大家作简单介绍,有需要的朋友,可以参考下。

使用php get_headers判断url的真实有效性。 还没有了解过php get_headers函数用法的朋友,可以参考本站文章:php get_headers函数讲解及用法介绍。 了解了该函数,大家便可以知道该函数会返回一个HTTP请求的头文件信息,信息格式基本如下: 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 )

2)、 Array ( [0] => HTTP/1.0 404 Not Found [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 )

分析: 如果判断该url是否有效存在,则通过数组中的第一个元素值来判断的。 服务器返回 200 即文件正确返回,返回 404 即文件不存在,这样就可以很容易判断一个url是否存在了。

很多时个,就是这些小函数帮了大忙,很多不错的获取header信息的小工具,原理与此基本相同,建议大家牢固掌握get_headers()函数的用法。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn