Two Methods for checking whether a link exists in php

Source: Internet
Author: User
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.

Related Article

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.