Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active April 30, 2019 13:21
Show Gist options
  • Save tommcfarlin/2ad96af9aa3007807686ac87b630dcd7 to your computer and use it in GitHub Desktop.
Save tommcfarlin/2ad96af9aa3007807686ac87b630dcd7 to your computer and use it in GitHub Desktop.
[PHP] Using cURL to Determine If the Specified URL a Valid Page
<?php
/**
* 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);
curl_close($curl);
return (404 !== $httpCode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment