Are there any best practices for efficiently checking server status using PHP?

When checking server status using PHP, it's important to efficiently handle the request to avoid unnecessary delays or timeouts. One best practice is to use cURL, a library that allows you to make HTTP requests in PHP. By setting appropriate options and handling errors gracefully, you can efficiently check the server status and respond accordingly.

$url = 'http://example.com/status_check'; // URL to check server status

$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);

if($response === false){
    echo 'Error: ' . curl_error($ch); // Handle error if request fails
} else {
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if($httpCode == 200){
        echo 'Server is up and running!';
    } else {
        echo 'Server returned HTTP code: ' . $httpCode;
    }
}

curl_close($ch);