php get_headers 判断URL是不是有效的方法

WBOY
Release: 2016-06-13 12:45:25
Original
765 people have browsed it

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()函数的用法。

本文出处参考:http://www.jbxue.com/article/7891.html

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template