health.php 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. if (php_sapi_name() !== 'cli') {
  5. echo 'Error: This script may only be invoked from command line!', PHP_EOL;
  6. die(2);
  7. }
  8. $options = getopt('', ['url::', 'connect_timeout::', 'timeout::']);
  9. $address = is_string($options['url'] ?? null) ? $options['url'] : 'http://localhost/i/';
  10. $ch = curl_init($address);
  11. if ($ch === false) {
  12. fwrite(STDERR, 'Error: Failed to initialize cURL!' . PHP_EOL);
  13. die(3);
  14. }
  15. curl_setopt_array($ch, [
  16. CURLOPT_CONNECTTIMEOUT => is_numeric($options['connect_timeout'] ?? null) ? (int)$options['connect_timeout'] : 3,
  17. CURLOPT_TIMEOUT => is_numeric($options['timeout'] ?? null) ? (int)$options['timeout'] : 5,
  18. CURLOPT_ENCODING => '', //Enable all encodings
  19. CURLOPT_HTTPHEADER => [
  20. 'Connection: close',
  21. ],
  22. CURLOPT_RETURNTRANSFER => true,
  23. ]);
  24. $content = curl_exec($ch);
  25. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  26. if ($httpCode !== 200 || !is_string($content) || !str_contains($content, 'jsonVars') || !str_contains($content, '</html>')) {
  27. die(1);
  28. }