Ejemplo n.º 1
0
 protected static function _send_request($url, $params = array(), $method = 'GET', $multi_part, $gzencoded = false, $has_return_value = true, $json_response = true, $has_api_response = true)
 {
     $response = null;
     $method = strtoupper($method);
     if ($params) {
         $url = self::add_parameters_to_url($url, $params);
         if ($method == 'GET') {
             $url .= '?' . http_build_query($params);
         }
     }
     if (!isset($params['api_version']) || !$params['api_version']) {
         $params['api_version'] = self::API_VERSION;
     }
     TranslationProxy_Com_Log::log_call($url, $params, $method, $multi_part);
     $api_response = self::_call_remote_api($url, $params, $method, $multi_part, $has_return_value);
     if ($gzencoded) {
         try {
             //					set_error_handler('icl_handleError');
             $gzdecoded_response = @gzdecode($api_response);
             //					restore_error_handler();
             if (!$gzdecoded_response) {
                 throw new TranslationProxy_Api_Error('gzdecode() returned an empty value. api_response: ' . print_r($api_response, true), 0);
             } else {
                 $api_response = $gzdecoded_response;
             }
         } catch (Exception $e) {
             throw new TranslationProxy_Api_Error('gzdecode() failed. api_response: ' . print_r($api_response, true), 0);
         }
     }
     if ($json_response) {
         TranslationProxy_Com_Log::log_response($api_response);
     } else {
         TranslationProxy_Com_Log::log_response('XLIFF received');
     }
     if ($has_return_value) {
         if ($json_response) {
             $response = json_decode($api_response);
             if ($has_api_response) {
                 $response = self::_get_api_response($response);
             }
         } else {
             $response = $api_response;
         }
     }
     return $response;
 }
 /**
  * @param string    $url
  * @param array     $params
  * @param string    $method
  * @param bool|true $has_return_value
  * @param bool|true $json_response
  * @param bool|true $has_api_response
  *
  * @return array|mixed|null|object|string
  * @throws TranslationProxy_Api_Error
  */
 public function send_request($url, $params = array(), $method = 'GET', $has_return_value = true, $json_response = true, $has_api_response = true)
 {
     if (!$url) {
         throw new InvalidArgumentException('Empty target URL given!');
     }
     $response = null;
     $method = strtoupper($method);
     if ($params) {
         $url = TranslationProxy_Api::add_parameters_to_url($url, $params);
         if ($method === 'GET') {
             $url .= '?' . http_build_query($params);
         }
     }
     if (!isset($params['api_version']) || !$params['api_version']) {
         $params['api_version'] = self::API_VERSION;
     }
     TranslationProxy_Com_Log::log_call($url, $params);
     $api_response = $this->call_remote_api($url, $params, $method, $has_return_value);
     if ($has_return_value) {
         if (!isset($api_response['headers']['content-type'])) {
             throw new RuntimeException('Invalid HTTP response, no content type in header given!');
         }
         $content_type = $api_response['headers']['content-type'];
         $api_response = $api_response['body'];
         $api_response = strpos($content_type, 'zip') !== false ? gzdecode($api_response) : $api_response;
         TranslationProxy_Com_Log::log_response($json_response ? $api_response : 'XLIFF received');
         if ($json_response) {
             $response = json_decode($api_response);
             if ($has_api_response) {
                 if (!$response || !isset($response->status->code) || $response->status->code != 0) {
                     throw new RuntimeException("Cannot communicate with the remote service response on url:" . $url . ' params: ' . serialize($params) . 'response: ' . serialize($response));
                 }
                 $response = $response->response;
             }
         } else {
             $response = $api_response;
         }
     }
     return $response;
 }