PHP detects if a link exists code instance sharing, PHP instance
In PHP, check whether a link exists, there are two methods, one is using curl, the other is
Get the HTTP header of the response code, if it is 200 is OK, if it is 404 can not 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 {
There is a 2nd argument in Get_headers, which is true, the result will be an associative 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 = =) { echo "URL Exists"} } else {
CURLOPT_NOBODY specifies that only the connection is established and the contents of the entire message are not taken.
http://www.bkjia.com/PHPjc/1123789.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123789.html techarticle PHP detection links exist code instance sharing, php instance in PHP, check whether a link exists, there are two methods, one is to use curl, the other is to get HTTP header response ...