Home >Backend Development >PHP Tutorial >Solution to use php get_headers to determine whether the URL is valid_PHP tutorial

Solution to use php get_headers to determine whether the URL is valid_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:11:01993browse

To determine whether a file or directory exists in PHP, everyone usually thinks of the two functions is_file and file_exists. However, these two functions still have problems in determining whether a remote url file exists. Here the author will share with you a way to use the php get_headers function to determine whether the remote url file is valid or exists.

For the function and usage of php get_headers function, you can refer to the article on this site:

Detailed introduction to the role and usage of the get_headers function in php

The following is a detailed description of how to use php get_headers to determine the true validity of the URL.

Through the introduction of this function, we can know that this function simply returns the header information of an HTTP request. The information format is basically as follows:

(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
)

It can be easily seen from the above two situations that if you judge whether the url is valid, it must be judged by the value of the first element in the array. The server returns 200, which means the file is returned correctly, and the server returns 404, which means the file does not exist, so from this place you can easily determine whether a URL exists.

(Detailed source: PHP Programmer’s Notes)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326936.htmlTechArticleTo determine whether a file or directory exists in PHP, everyone usually thinks of the two functions is_file and file_exists. But these two functions also determine whether a remote url file exists...
Statement:
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