<?php
namespace App\Http\Controllers;
class
HealthChecker
{
public static function check($webhookUrl, $monitoredUrl, $onlyError
=
false)
{
$json
=
[];
$json[
'webhookUrl'
]
=
$webhookUrl;
$json[
'monitoredUrl'
]
=
$monitoredUrl;
$res
=
@file_get_contents($monitoredUrl, false);
if
($res) {
if
($onlyError) {
return
$json;
}
$json[
'health'
]
=
"up"
;
$json[
'body'
]
=
$res;
if
(isset($http_response_header)) {
$headers
=
[];
$json[
'message'
]
=
$http_response_header[
0
];
for
($i
=
0
; $i <
=
count($http_response_header)
-
1
; $i
+
+
) {
$split
=
explode(
':'
, $http_response_header[$i],
2
);
if
(count($split)
=
=
2
) {
$headers[trim($split[
0
])]
=
trim($split[
1
]);
}
else
{
error_log(
"invalid header pair: $http_response_header[$i]\n"
);
}
}
$json[
'headers'
]
=
$headers;
}
}
else
{
$json[
'health'
]
=
"down"
;
}
$content
=
json_encode($json);
/
/
send
$curl
=
curl_init($webhookUrl);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array(
"Content-type: application/json"
));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_exec($curl);
curl_close($curl);
return
$json;
}
}