How do I check if a URL exists (not 404) in PHP?
I run some tests to see if links on my site are valid - alerts me to when third parties change their links. I was having an issue with a site that had a poorly configured certificate that meant that php's get_headers didn't work.
SO, I read that curl was faster and decided to give that a go. then i had an issue with linkedin which gave me a 999 error, which turned out to be a user agent issue.
I didn't care if the certificate was not valid for this test, and i didn't care if the response was a re-direct.
Then I figured use get_headers anyway if curl was failing....
Give it a go....
/**
* returns true/false if the $url is present.
*
* @param string $url assumes this is a valid url.
*
* @return bool
*/
private function url_exists (string $url): bool
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // this does a head request to make it faster.
curl_setopt($ch, CURLOPT_HEADER, TRUE); // just the headers
curl_setopt($ch, CURLOPT_SSL_VERIFYSTATUS, FALSE); // turn off that pesky ssl stuff - some sys admins can't get it right.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// set a real user agent to stop linkedin getting upset.
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36');
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (($http_code >= HTTP_OK && $http_code < HTTP_BAD_REQUEST) || $http_code === 999)
{
curl_close($ch);
return TRUE;
}
$error = curl_error($ch); // used for debugging.
curl_close($ch);
// just try the get_headers - it might work!
stream_context_set_default(array('http' => array('method' => 'HEAD')));
$file_headers = @get_headers($url);
if ($file_headers)
{
$response_code = substr($file_headers[0], 9, 3);
return $response_code >= 200 && $response_code < 400;
}
return FALSE;
}