No need to use CURL, file_get_contents($url);
will return false if the request fails (any other HTTP code other than 2xx), which might be more useful for what you're trying to do, an example:
function urlExists($url)
{
return (bool) @file_get_contents($url);
}
Will return true if the URL returns useful content, false otherwise.
EDIT: Here is a faster way (it only requests the headers) and the first byte instead of the whole page:
function urlExists($url)
{
return (bool) @file_get_contents($url, false, null, 0, 1);
}
urlExists('http://stackoverflow.com/iDontExist'); // false
However, in combination with your other question it may be wiser to use something like this:
function url($url)
{
return @file_get_contents($url);
}
$content = url('http://stackoverflow.com/');
// request has failed (404, 5xx, etc...)
if ($content === false)
{
// delete or store as "failed" in the DB
}
// request was successful
else
{
$hash = md5($content); // md5() should be enough but you can also use sha1()
// store $hash in the DB to keep track of changes
}
Or if you're using PHP 5.1+ you only have to do:
$hash = @md5_file($url);
$hash
will be false when the URL fails to load, otherwise it will return the MD5 hash of the contents.
Graciously stolen from @Jamie. =)
This way you only have to make one request instead of two. =)