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