Use phpget_headers to determine whether the URL is valid _ php instance

Source: Internet
Author: User
This article describes how to use phpget_headers to determine whether a URL is valid. If you want to determine whether a file or directory exists in php, you will usually think of the is_file and file_exists functions. However, the two functions determine whether a remote url file exists. The author will share with you a way to use the php get_headers function to determine whether a remote url file exists effectively.

For the functions and usage of the php get_headers function, refer to this article:

The role and usage of the get_headers function in php

The following describes how to use php get_headers to determine the true validity of a url.

Through the introduction of this function, we can know that this function simply returns the header file 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
)

From the above two cases, we can easily see that, If you determine whether the url is valid, it must be determined by the first element value in the array. If the server returns 200, that is, the file is returned correctly. If the server returns 404, that is, the file does not exist, you can easily determine whether a url exists.

(Detailed Source: PHP programmer notes)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.