Skip to content

Instantly share code, notes, and snippets.

Last active Apr 30, 2019
What would you like to do?
[PHP] Using cURL to Determine If the Specified URL a Valid Page
* Determines if a specific URL returns a valid page. This is experimental and it is based on
* the status code.
* @param string $url the url to evaluate
* @return bool true if the URL returns a status code of 404; otherwise, false
public function isValidUrl(string $url): bool
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
return (404 !== $httpCode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment