Are there any best practices for handling server status checks in PHP?

When handling server status checks in PHP, it is important to use asynchronous requests to avoid blocking the server and affecting performance. One common approach is to use cURL to send HTTP requests to the server and check for the response status code. Additionally, implementing a timeout mechanism can prevent the script from waiting indefinitely for a response.

<?php

// Function to check server status
function checkServerStatus($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); // Set timeout to 5 seconds
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode == 200) {
        echo "Server is up and running.";
    } else {
        echo "Server is down.";
    }
}

// Example usage
checkServerStatus("http://example.com");

?>