Game server and admin dashboard for TerranQuest.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccountHubApi.lib.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. */
  7. class AccountHubApi {
  8. public static function get(string $action, array $data = null, bool $throwex = false) {
  9. $content = [
  10. "action" => $action,
  11. "key" => PORTAL_KEY
  12. ];
  13. if (!is_null($data)) {
  14. $content = array_merge($content, $data);
  15. }
  16. $options = [
  17. 'http' => [
  18. 'method' => 'POST',
  19. 'content' => json_encode($content),
  20. 'header' => "Content-Type: application/json\r\n" .
  21. "Accept: application/json\r\n",
  22. "ignore_errors" => true
  23. ]
  24. ];
  25. $context = stream_context_create($options);
  26. $result = file_get_contents(PORTAL_API, false, $context);
  27. $response = json_decode($result, true);
  28. if ($result === false || !AccountHubApi::checkHttpRespCode($http_response_header) || json_last_error() != JSON_ERROR_NONE) {
  29. if ($throwex) {
  30. throw new Exception($result);
  31. } else {
  32. sendError($result);
  33. }
  34. }
  35. return $response;
  36. }
  37. private static function checkHttpRespCode(array $headers): bool {
  38. foreach ($headers as $header) {
  39. if (preg_match("/HTTP\/[0-9]\.[0-9] [0-9]{3}.*/", $header)) {
  40. $respcode = explode(" ", $header)[1] * 1;
  41. if ($respcode >= 200 && $respcode < 300) {
  42. return true;
  43. }
  44. }
  45. }
  46. return false;
  47. }
  48. }