overhauled error check

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-23 16:28:46 +01:00
parent eb56d78a9e
commit 28b2738c3e
1 changed files with 9 additions and 13 deletions

View File

@ -15,15 +15,14 @@ class CheckController
* @param String $apiKey * @param String $apiKey
* @param String $command * @param String $command
* *
* @return bool|array * @return String
*/ */
function sendCommand(String $serverName, int $versionIP, String $apiKey, String $command) : Bool|Array function sendCommand(String $serverName, int $versionIP, String $apiKey, String $command): String
{ {
$result = false;
$curl = curl_init(); $curl = curl_init();
curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command); curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command);
curl_setopt($curl, option: CURLOPT_RETURNTRANSFER, value: 1); curl_setopt($curl, option: CURLOPT_RETURNTRANSFER, value: 1);
curl_setopt($curl, option: CURLOPT_TIMEOUT_MS, value: 2000); curl_setopt($curl, option: CURLOPT_TIMEOUT_MS, value: 1000);
if ($versionIP == 4) { if ($versionIP == 4) {
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4); curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
@ -41,26 +40,23 @@ class CheckController
$apiResult = json_decode($resultJSON); $apiResult = json_decode($resultJSON);
if ($command == "ping" ) { if ($command == "ping" ) {
if ($apiResult->response == "pong") { if ($apiResult->response == "pong") {
$result = true; $result = $apiResult->response;
} else { } else {
print("Error: $httpResponse" . PHP_EOL); $result = $resultJSON;
} }
} else { } else {
$result = $apiResult; $result = $resultJSON;
} }
break; break;
case 401: case 401:
print("Missing or wrong API Key" . PHP_EOL); $result = 'Missing or wrong API Key';
break; break;
default: default:
print("Unhandled error: " . $httpResponse . PHP_EOL); $result = 'Unhandled error: ' . $httpResponse;
} }
} else { } else {
$error = curl_error($curl); $result = curl_error($curl);
print("Unknown error: $error" . PHP_EOL);
} }
curl_close($curl); curl_close($curl);
return $result; return $result;
} }