Is using cURL to retrieve the HTTP status the most reliable method to check the existence of a website URL in PHP?
Using cURL to retrieve the HTTP status of a website URL is a reliable method to check its existence in PHP. By sending a HEAD request using cURL, we can efficiently check the status code returned by the server. If the status code is in the 200 range, it indicates that the URL exists and is accessible.
$url = "https://www.example.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 200 && $httpCode < 300) {
echo "URL exists and is accessible.";
} else {
echo "URL does not exist or is not accessible.";
}
Keywords
Related Questions
- What are some best practices for integrating PayPal payment confirmation into a PHP website?
- What are the potential risks of using outdated PHP functions like mysql_real_escape_string for database operations?
- What are some alternative approaches to deleting database records in PHP that may be more efficient or secure than the method described in the forum thread?