Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin tommcfarlin/00-is-valid-url.php Secret
Last active Apr 30, 2019

Embed
What would you like to do?
[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
You can’t perform that action at this time.