-
-
Save tommcfarlin/2ad96af9aa3007807686ac87b630dcd7 to your computer and use it in GitHub Desktop.
[PHP] Using cURL to Determine If the Specified URL a Valid Page
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
<?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