Game server and admin dashboard for TerranQuest.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AccountHubApi.lib.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. global $SETTINGS;
  10. $content = [
  11. "action" => $action,
  12. "key" => $SETTINGS['accounthub']['key']
  13. ];
  14. if (!is_null($data)) {
  15. $content = array_merge($content, $data);
  16. }
  17. $options = [
  18. 'http' => [
  19. 'method' => 'POST',
  20. 'content' => json_encode($content),
  21. 'header' => "Content-Type: application/json\r\n" .
  22. "Accept: application/json\r\n",
  23. "ignore_errors" => true
  24. ]
  25. ];
  26. $context = stream_context_create($options);
  27. $result = file_get_contents($SETTINGS['accounthub']['api'], false, $context);
  28. $response = json_decode($result, true);
  29. if ($result === false || !AccountHubApi::checkHttpRespCode($http_response_header) || json_last_error() != JSON_ERROR_NONE) {
  30. if ($throwex) {
  31. throw new Exception($result);
  32. } else {
  33. sendError($result);
  34. }
  35. }
  36. return $response;
  37. }
  38. private static function checkHttpRespCode(array $headers): bool {
  39. foreach ($headers as $header) {
  40. if (preg_match("/HTTP\/[0-9]\.[0-9] [0-9]{3}.*/", $header)) {
  41. $respcode = explode(" ", $header)[1] * 1;
  42. if ($respcode >= 200 && $respcode < 300) {
  43. return true;
  44. }
  45. }
  46. }
  47. return false;
  48. }
  49. }