How do I check if a URL exists (not 404) in PHP?
The best and simplest answer so far using get_headers() The best thing to check for string "200 ok". its far better than to check
$file_headers = @get_headers($file-path);
$file_headers[0];
because sometime the array key numbers varies. so best thing is to check for "200 ok". Any URL which is up will have "200 ok" anywhere in get_headers() response.
function url_exist($url) {
$urlheaders = get_headers($url);
//print_r($urlheaders);
$urlmatches = preg_grep('/200 ok/i', $urlheaders);
if(!empty($urlmatches)){
return true;
}else{
return false;
}
}
now check the function if true or false
if(url_exist(php-url-variable-here)
URL exist
}else{
URL don't exist
}