Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic health status check using PHP curl
<?php
$responseCode = 500;
$responseCodeOk = 200;
$responseCodeFail = 503;
$url = 'https://www.deathe.org/';
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_URL => $url,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => 'Custom Status Check',
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 10,
CURLOPT_NOBODY => true,
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true
)
);
curl_exec(
$ch
);
if (
curl_errno(
$ch
) === 0
) {
$responseCode = curl_getinfo(
$ch,
CURLINFO_RESPONSE_CODE
);
}
curl_close(
$ch
);
header(
'Cache-Control: no-cache'
);
header(
'Content-Type: text/plain'
);
if (
preg_match(
'/^[23][0-9]{2}/',
$responseCode
) !== 1
) {
http_response_code(
$responseCodeFail
);
exit;
}
http_response_code(
$responseCodeOk
);
print 'OK';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment