Skip to content

Instantly share code, notes, and snippets.

@kaerimichi
Last active March 24, 2017 16:31
Show Gist options
  • Save kaerimichi/5dfb4e42a3ede4a705fbcde6f9683051 to your computer and use it in GitHub Desktop.
Save kaerimichi/5dfb4e42a3ede4a705fbcde6f9683051 to your computer and use it in GitHub Desktop.
Simple URL health check queue based on HTTP status code (using PHP and cURL)
#!/usr/bin/php
<?php
$urlsFile = file_get_contents('./healthcheck.txt');
$urls = split("\n", $urlsFile);
$filteredList = array_filter($urls, function ($entry) {
return !empty($entry) && substr($entry, 0, 1) != '#';
});
$failedRequests = (bool) array_reduce($filteredList, function ($acc, $entry) {
$url = split(' ', $entry)[0];
$expectedCode = split(' ', $entry)[1];
$statusCode = (int) exec('curl -s -o /dev/null -w "%{http_code}" ' . $url);
echo "{$url} >>> {$statusCode}/{$expectedCode}\n";
if ($expectedCode != $statusCode) $acc++;
return $acc;
}, 0);
if ($failedRequests) exit(1);
exit(0);
# the script will finish with 0 if all URL status codes have matched the expected code
https://google.com 200
https://github.com 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment