How can PHP be used to automate the process of checking server and service status regularly?

To automate the process of checking server and service status regularly using PHP, we can create a script that makes HTTP requests to the servers/services and checks for expected responses or status codes. This script can be scheduled to run at regular intervals using a cron job or a task scheduler.

<?php
// List of servers/services to check
$servers = array(
    'http://example.com',
    'http://api.example.com',
    'http://database.example.com'
);

// Loop through each server/service
foreach ($servers as $server) {
    $response = file_get_contents($server);

    // Check for expected response or status code
    if ($response === false) {
        echo "Server $server is down\n";
    } else {
        echo "Server $server is up\n";
    }
}
?>