Php Tutorial: Check whether the URL exists

Source: Internet
Author: User
We usually don't often think of checking the validity of a URL address, or don't care about its existence or non-existence: Because there is basically no such application on weekdays. Today, let's see how to use PHP to check whether a URL exists. Here, we will use two methods. One is to get the HTTP header Through the get_headers function.

We usually don't often think of checking the validity of a URL address, or don't care about its existence or non-existence: Because there is basically no such application on weekdays. Today, let's see how to use PHP to check whether a URL exists. Here, we will use two methods. One is to get the HTTP header Through the get_headers function.

We usually don't often think of checking the validity of a URL address, or don't care about its existence or non-existence: Because there is basically no such application on weekdays. Today, let's see how to use PHP to check whether a URL exists. Here, we will use two methods: one is to get the HTTP header information through the get_headers function to make a judgment; the other is to use curl to construct a capture response code returned.

Get_headers version
$ Url = "http://www.domain.com/demo.jpg ";?? $ Headers = @ get_headers ($ url); // capture HTTP? Header? Information ?? If (strpos ($ headers [0], '000000') = false) {// check if there is any 404 ???? Echo? "The specified URL exists and is valid ";??} Else {???? Echo? "The specified URL is not born ";??}??
CURL version
$ Url? =? "Http://www.domain.com/demo.jpg ";?? $ Curl? =? Curl_init ($ url );?? Curl_setopt ($ curl ,? CURLOPT_NOBODY ,? True );?? $ Result? =? Curl_exec ($ curl );?? If? ($ Result ?! ==? False ){???? $ StatusCode? =? Curl_getinfo ($ curl ,? CURLINFO_HTTP_CODE );?? ???? If? ($ StatusCode? ==? 404 ){?????? Echo? "Enter a URL that does not exist ";????} Else {??????? Echo? "The specified URL already exists ";????}? ??} Else {???? Echo? "The URL does not exist ";??}??

Statement: This article uses BY-NC-SA protocol for authorization | IT passers-
Reprinted please note

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.