Created
September 4, 2015 20:27
-
-
Save thagxt/7a2f20549c4636e9d584 to your computer and use it in GitHub Desktop.
php function to check if URL is 404 or nah
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not working!