Skip to content

Instantly share code, notes, and snippets.

@Scemist
Last active November 17, 2022 12:49
Show Gist options
  • Save Scemist/0e839d8b15336be5e007275436a6d7be to your computer and use it in GitHub Desktop.
Save Scemist/0e839d8b15336be5e007275436a6d7be to your computer and use it in GitHub Desktop.
PHP Cli Request Loop
<?php
# FileName should be "call", without extension. A calling in cli like "php call google.com"
enum Color: string
{
case Black = "\033[30m";
case Red = "\033[31m";
case Green = "\033[32m";
case Orange = "\033[33m";
case Blue = "\033[34m";
case Magenta = "\033[35m";
case Cyan = "\033[36m";
case LightGray = "\033[37m";
case Default = "\033[39m";
case BBlack = "\033[40m";
case BRed = "\033[41m";
case BGreen = "\033[42m";
case BOrange = "\033[43m";
case BBlue = "\033[44m";
case BMagenta = "\033[45m";
case BCyan = "\033[46m";
case BLightGray = "\033[47m";
case BDefault = "\033[49m";
case ADefault = "\033[39m\033[49m";
}
$arguments = $_SERVER['argv'];
$url = $arguments[1];
$interval = 180;
if (substr($url, 0, 8) !== 'https://')
$url = "https://$url";
echo "Requesting each $interval seconds to $url", PHP_EOL;
while (true) {
$context = stream_context_create(['http' => ['ignore_errors' => true]]);
$response = file_get_contents($url, false, $context);
preg_match('{HTTP\/\S*\s(\d{3})}', $http_response_header[0], $match);
$status = (string) $match[1];
echo match ($status[0]) {
'2' => Color::Green->value,
'4', '5' => Color::Orange->value,
default => Color::Red->value,
},
date('H:i:s'), ' Returned status ', $status,
Color::ADefault->value, PHP_EOL;
if ($status == 201)
continue;
sleep($interval);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment