First request - PocketCloudSystem/PocketCloud GitHub Wiki

Request function

So first, we need to make a function to create an HTTP request.

function createRequest(string $route, string $requestMethod, array $queries = []): array|false {
    $url = "http://yourip:yourport/" . $route . (count($queries) > 0 ? "?" . http_build_query($queries) : "");
    $authKey = "your_auth_key";

    $extraOptions = [];
    if ($requestMethod == "POST") $extraOptions = [CURLOPT_POSTFIELDS => http_build_query($queries), CURLOPT_POST => true];
    else if ($requestMethod == "PATCH") $extraOptions = [CURLOPT_POSTFIELDS => http_build_query($queries), CURLOPT_CUSTOMREQUEST => "PATCH"];
    else if ($requestMethod == "DELETE") $extraOptions = [CURLOPT_CUSTOMREQUEST => "DELETE"];

    $ch = curl_init($url);
    curl_setopt_array($ch, [
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => true,
            CURLOPT_HTTPHEADER => ["auth-key: " . $authKey]
        ] + $extraOptions
    );

    $result = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
    if (!$result) throw new \Exception("HTTP Request to " . $requestMethod . " " . $url . " failed: " . curl_error($ch), $code ?? 0);

    $header = explode("\r\n", substr($result, 0, ($len = curl_getinfo($ch, CURLINFO_HEADER_SIZE))));
    $body = substr($result, $len);

    if (($decoded = json_decode($body, true)) === null && $decoded === false) return [$code, $requestMethod, $header, $body];
    //"$code" is the returned http status code
    //"$requestMethod" is the provided request method
    //"$decoded" is the decoded json response and "$body" is the raw json response
    return [$code, $requestMethod, $decoded];
}

Our first request

With this code snippet, we can debug all the response-data.

var_dump(createRequest("cloud/info/", "GET"));

Output example:

array(3) {
  [0]=>
  int(200)
  [1]=>
  string(3) "GET"
  [2]=>
  array(9) {
    ["version"]=>
    string(5) "1.1.1"
    ["version_developer"]=>
    array(1) {
      [0]=>
      string(6) "r3pt1s"
    }
    ["templates"]=>
    array(2) {
      [0]=>
      string(5) "Lobby"
      [1]=>
      string(5) "Proxy"
    }
    ["runningServers"]=>
    array(2) {
      [0]=>
      string(7) "Lobby-1"
      [1]=>
      string(7) "Proxy-1"
    }
    ["players"]=>
    array(0) {
    }
    ["loadedPlugins"]=>
    array(0) {
    }
    ["enabledPlugins"]=>
    array(0) {
    }
    ["disabledPlugins"]=>
    array(0) {
    }
    ["network_address"]=>
    string(13) "127.0.0.1:19456"
  }
}