Last active
February 8, 2019 19:45
-
-
Save jdeathe/c96f20e700634ac6c8fe51f9dcfb62a8 to your computer and use it in GitHub Desktop.
Basic health status check using PHP curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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