This article mainly introduces PHP code examples for checking whether a link exists. it is very convenient to use cURL. For more information, see in PHP, check whether a link exists. There are two methods: curl and
Obtain the response code of the HTTP header. if it is 200, it is OK. if it is 404, it cannot be found. The example is as follows:
1) use get_headers:
<? Php $ url = "http://www.abc.com/demo.jpg"; $ headers = @ get_headers ($ url); if ($ headers [0] = 'http/1.1 404 Not Found ') {echo "URL not Exists" ;}else {echo "URL Exists" ;}?>
Get_headers has 2nd parameters. if it is true, the result will be an associated array.
2) use CURL
<? Php $ 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 = 200) {echo "URL Exists" }}else {echo "URL not Exists" ;}?>
CURLOPT_NOBODY specifies that the connection is established instead of the content of the entire message.