Files
azionelab-v2/wordpress/healthcheck.php
T

37 lines
1000 B
PHP

<?php
declare(strict_types=1);
$wpUrl = getenv('WP_URL') ?: 'http://localhost';
$parts = parse_url($wpUrl);
$host = $parts['host'] ?? 'localhost';
$scheme = $parts['scheme'] ?? 'http';
$port = isset($parts['port']) ? (int) $parts['port'] : null;
$hostHeader = $host;
if ($port !== null && !(($scheme === 'http' && $port === 80) || ($scheme === 'https' && $port === 443))) {
$hostHeader .= ':' . $port;
}
$context = stream_context_create([
'http' => [
'follow_location' => 0,
'header' => "Host: {$hostHeader}\r\nX-Forwarded-Proto: {$scheme}\r\n",
'ignore_errors' => true,
'max_redirects' => 0,
'timeout' => 3,
],
]);
$response = @file_get_contents('http://127.0.0.1/wp-login.php', false, $context);
if ($response === false || empty($http_response_header[0])) {
exit(1);
}
if (!preg_match('/\s([0-9]{3})\s/', $http_response_header[0], $matches)) {
exit(1);
}
$status = (int) $matches[1];
exit($status >= 200 && $status < 400 ? 0 : 1);