initial commit

This commit is contained in:
tracer 2022-01-22 16:37:00 +01:00
parent 6cf4c83593
commit 63c5eacb95
1 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,68 @@
<?php
namespace App\Controller;
/**
*
*/
class CheckController
{
/**
* @param String $serverName
* @param int $versionIP
* @param String $apiKey
* @param String $command
*
* @return bool|array
*/
function sendCommand(String $serverName, int $versionIP, String $apiKey, String $command) : Bool|Array
{
$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);
if ($versionIP == 4) {
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
} else {
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
}
curl_setopt($curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
if ($resultJSON = curl_exec($curl)) {
$httpResponse = curl_getinfo($curl)['http_code'];
switch($httpResponse) {
case 200:
$apiResult = json_decode($resultJSON);
if ($command == "ping" ) {
if ($apiResult->response == "pong") {
$result = true;
} else {
print("Error: $httpResponse" . PHP_EOL);
}
} else {
$result = $apiResult;
}
break;
case 401:
print("Missing or wrong API Key" . PHP_EOL);
break;
default:
print("Unhandled error: " . $httpResponse . PHP_EOL);
}
} else {
$error = curl_error($curl);
print("Unknown error: $error" . PHP_EOL);
}
curl_close($curl);
return $result;
}
}