PHP checks whether there are code instance sharing links, and php instances. PHP checks whether a link exists and shares code instances. in php, PHP checks whether a link exists. There are two methods: use curl, the other is to obtain the response of the HTTP header. PHP checks whether the link exists.
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:
<?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.
In PHP, the handler checks whether a link exists. There are two methods: using curl and obtaining the response of the HTTP header...