Advertisement
aivavic

Untitled

Jan 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. function slack($message, $channel)
  2. {
  3.     $token = $_SERVER['TOKEN'];
  4.     $ch = curl_init("https://slack.com/api/chat.postMessage");
  5.     $data = http_build_query([
  6.         "token" => $token,
  7.         "channel" => $channel, //"#mychannel",
  8.         "text" => $message, //"Hello, Foo-Bar channel message.",
  9.         "username" => "ExternalHealthCheckBot",
  10.     ]);
  11.     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  12.     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  13.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  15.     $result = curl_exec($ch);
  16.     curl_close($ch);
  17.  
  18.     return $result;
  19. }
  20.  
  21. function healthCheck() {
  22.     $url = $_SERVER['CHECK_URL'];
  23.     $ch = curl_init($url);
  24.     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  25.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  26.     curl_setopt($ch, CURLOPT_HTTPHEADER,
  27.             ['Content-Type: application/json']
  28.     );
  29.     $result =  curl_exec($ch);
  30.  
  31.     if (!curl_errno($ch)) {
  32.         switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
  33.             case 200: #OK
  34.                break;
  35.             default:
  36.                 $str = json_encode(['code' => $http_code, 'response' => $result]);
  37.                 slack($str, 'check');
  38.         }
  39.     } else {
  40.         slack(curl_error($ch), 'check');
  41.     }
  42.  
  43.     curl_close($ch);
  44. }
  45.  
  46. healthCheck();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement