protected function process(string $contentType, string $data) : string
{
// Server address must be defined
if (empty($this->url)) {
throw new \BadMethodCallException('No server address was provided.');
}
// Headers
$headers = ['Content-Type: ' . $contentType, 'Content-Length: ' . strlen($data)];
$defaultCurlOptions = [CURLOPT_URL => $this->url, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 0, CURLOPT_TIMEOUT => $this->timeout, CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $data];
$curlOptions = $this->curlOptions + $defaultCurlOptions;
// Open a HTTP channel
$channel = curl_init();
foreach ($curlOptions as $key => $value) {
curl_setopt($channel, $key, $value);
}
// Send a request
$response = curl_exec($channel);
// Error sending the request
if (0 !== curl_errno($channel)) {
$error = curl_error($channel);
curl_close($channel);
throw new Exception($error);
}
// Wrong code
$code = curl_getinfo($channel, CURLINFO_HTTP_CODE);
if ($code >= 300) {
$error = sprintf('Response error from %s, code %d.', $this->url, $code);
curl_close($channel);
throw new Exception($error);
}
// Close the channel
curl_close($channel);
// Return the response
return $response;
}