Created
May 7, 2021 16:37
-
-
Save Llewellynvdm/4c78638b418f641e7bae1e844be5acf4 to your computer and use it in GitHub Desktop.
urlExists
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
/** | |
* 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