Skip to content

Instantly share code, notes, and snippets.

@jdeathe
Last active February 8, 2019 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdeathe/c96f20e700634ac6c8fe51f9dcfb62a8 to your computer and use it in GitHub Desktop.
Save jdeathe/c96f20e700634ac6c8fe51f9dcfb62a8 to your computer and use it in GitHub Desktop.
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