Example #1
0
 public static function call($json_request)
 {
     $json_reply = null;
     for ($i = 0; $i < 3; ++$i) {
         try {
             $doc = HD::http_post_document(static::API_URL, json_encode($json_request));
             $json_reply = json_decode($doc);
         } catch (Exception $e) {
             hd_print('Error: failed to do HTTP-request.');
             if ($i == 2) {
                 throw new DuneException('API is unavailable', 0, ActionFactory::show_error(true, 'Системная ошибка', array('Сервер недоступен(' . $e->getMessage() . ').', 'Пожалуйста обратитесь в тех. поддержку.')));
             }
             usleep(100000 << $i);
             continue;
         }
         break;
     }
     if (is_null($json_reply)) {
         hd_print("Error: failed to decode API reply: '{$doc}'");
         throw new DuneException('API returned empty result', 0, ActionFactory::show_error(true, 'Системная ошибка', array('Сервер вернул пустой ответ.', 'Пожалуйста обратитесь в тех. поддержку.')));
     }
     if (isset($json_reply->error) && $json_reply->error) {
         hd_print("Error: API returned error status({$doc})");
         throw new DuneException('API returned error', $json_reply->code, ActionFactory::show_error(true, 'Системная ошибка', array('Сервер вернул ошибку(' . $json_reply->message . ').', 'Пожалуйста обратитесь в тех. поддержку.')));
     }
     // TODO: Think of a better check.
     if (!isset($json_request['method'])) {
         $request_count = count($json_request);
         $reply_count = count($json_reply);
         if ($request_count != $reply_count) {
             return false;
         }
         for ($i = 0, $n = 0; $i < $reply_count; $n = $n + ($json_reply[$i]->result ? 1 : 0), $i++) {
         }
         if ($i != $n) {
             return false;
         }
     }
     return $json_reply;
 }