From 28b2738c3e4ba5c57f6e9e053a63bb88e85db2c3 Mon Sep 17 00:00:00 2001 From: tracer Date: Sun, 23 Jan 2022 16:28:46 +0100 Subject: [PATCH] overhauled error check Signed-off-by: tracer --- bindAPI/src/Controller/CheckController.php | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/bindAPI/src/Controller/CheckController.php b/bindAPI/src/Controller/CheckController.php index 6f9d4e2..473cdff 100644 --- a/bindAPI/src/Controller/CheckController.php +++ b/bindAPI/src/Controller/CheckController.php @@ -15,15 +15,14 @@ class CheckController * @param String $apiKey * @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_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command); 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) { curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4); @@ -41,26 +40,23 @@ class CheckController $apiResult = json_decode($resultJSON); if ($command == "ping" ) { if ($apiResult->response == "pong") { - $result = true; + $result = $apiResult->response; } else { - print("Error: $httpResponse" . PHP_EOL); + $result = $resultJSON; } } else { - $result = $apiResult; + $result = $resultJSON; } break; case 401: - print("Missing or wrong API Key" . PHP_EOL); + $result = 'Missing or wrong API Key'; break; default: - print("Unhandled error: " . $httpResponse . PHP_EOL); + $result = 'Unhandled error: ' . $httpResponse; } - } else { - $error = curl_error($curl); - print("Unknown error: $error" . PHP_EOL); + $result = curl_error($curl); } - curl_close($curl); return $result; }