Skip to content

Instantly share code, notes, and snippets.

@Llewellynvdm
Created May 7, 2021 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Llewellynvdm/4c78638b418f641e7bae1e844be5acf4 to your computer and use it in GitHub Desktop.
Save Llewellynvdm/4c78638b418f641e7bae1e844be5acf4 to your computer and use it in GitHub Desktop.
urlExists
/**
* Check if the url exist
*
* @param string $url The url to check
*
* @return bool If exist true
*
*/
public static function urlExists($url)
{
$exists = false;
// check if we can use curl
if (function_exists('curl_version'))
{
// initiate curl
$ch = curl_init($url);
// CURLOPT_NOBODY (do not return body)
curl_setopt($ch, CURLOPT_NOBODY, true);
// make call
$result = curl_exec($ch);
// check return value
if ($result !== false)
{
// get the http CODE
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($statusCode !== 404)
{
$exists = true;
}
}
// close the connection
curl_close($ch);
}
elseif ($headers = @get_headers($url))
{
if(isset($headers[0]) && is_string($headers[0]) && strpos($headers[0],'404') === false)
{
$exists = true;
}
}
return $exists;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment