/** * @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|stdClass|string * @throws TranslationProxy_Api_Error * @throws RuntimeException * @throws InvalidArgumentException */ 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; } WPML_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; WPML_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) { $sanitized_url = WPML_TranslationProxy_Com_Log::sanitize_url($url); $sanitized_params = WPML_TranslationProxy_Com_Log::sanitize_data($params); $sanitized_response = WPML_TranslationProxy_Com_Log::sanitize_data($response); $exception_message = 'Cannot communicate with the remote service response on url: `' . $sanitized_url . '` params: `' . serialize($sanitized_params) . '` response: `' . serialize($sanitized_response) . '`'; if (isset($response->status->message)) { $exception_message = ''; if (isset($response->status->code)) { $exception_message = '(' . $response->status->code . ') '; } $exception_message .= $response->status->message; } throw new RuntimeException($exception_message); } $response = $response->response; } } else { $response = $api_response; } } return $response; }
public 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 = 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, $method, $multi_part); $api_response = $this->call_remote_api($url, $params, $method, $multi_part, $has_return_value); if ($gzencoded) { try { $gzdecoded_response = @gzdecode($api_response); 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); } } TranslationProxy_Com_Log::log_response($json_response ? $api_response : 'XLIFF received'); if ($has_return_value) { if ($json_response) { $response = json_decode($api_response); if ($has_api_response) { $response = $this->get_api_response($response); } } else { $response = $api_response; } } return $response; }
private function _create_iframe_url($url, $params) { try { if ($params) { $url = TranslationProxy_Api::add_parameters_to_url($url, $params); $url .= '?' . http_build_query($params); } return $url; } catch (Exception $ex) { $this->add_error($ex->getMessage()); return false; } }
private function _create_iframe_url($url, $params) { if ($params) { $url = TranslationProxy_Api::add_parameters_to_url($url, $params); $url .= '?' . http_build_query($params); } return $url; }