Skip to content

Instantly share code, notes, and snippets.

@thagxt
Created September 4, 2015 20:27
Show Gist options
  • Save thagxt/7a2f20549c4636e9d584 to your computer and use it in GitHub Desktop.
Save thagxt/7a2f20549c4636e9d584 to your computer and use it in GitHub Desktop.
php function to check if URL is 404 or nah
<?php
// check if URL exists
function checkUrl($url) {
// Simple check
if (!$url) { return FALSE; }
// Create cURL resource using the URL string passed in
$curl_resource = curl_init($url);
// Set cURL option and execute the "query"
curl_setopt($curl_resource, CURLOPT_RETURNTRANSFER, true);
curl_exec($curl_resource);
// Check for the 404 code (page must have a header that correctly display 404 error code according to HTML standards
if(curl_getinfo($curl_resource, CURLINFO_HTTP_CODE) == 404) {
// Code matches, close resource and return false
curl_close($curl_resource);
return FALSE;
} else {
// No matches, close resource and return true
curl_close($curl_resource);
return TRUE;
}
// Should never happen, but if something goofy got here, return false value
return FALSE;
}
// checking
if (checkUrl($country_url) == false) {
echo "404 not found";
echo $search;
} else {
echo "200 OK";
echo $country_url;
}
?>
@BartusZak
Copy link

Not working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment