Php checks whether a link exists in PHP by using two methods: curl and HTTP header response code, if it is 200, it is OK. If it is 404, it cannot be found. Example: 1) Use get_headers: & nbsp; & lt ;? Php $ urlwww.abc.comdemo.jp two methods for checking whether a link exists in php $ urlwww.abc.comdemo.jp
In PHP, there are two methods to check whether a link exists. One is to use curl and the other is
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:
$ 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
$ 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.