private function __setHeaders($ch, $add_headers = array(), $data = array())
{
$content_length = 0;
if (count($data)) {
$content_length = strlen(json_encode($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
// path
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$url_parts = parse_url($url);
if ($url_parts === false) {
throw new \Exception('Incorrect URL ' . $url);
}
$path = $url_parts['path'] . (!empty($url_parts['query']) ? '?' . $url_parts['query'] : '');
// content
$content_type = 'application/json';
$content = $data ? json_encode($data) : '';
$content_md5 = md5(utf8_encode($content));
// date
$date = gmdate('D, d M Y H:i:s \\G\\M\\T');
// sign string
$sign_string = join("\n", array($this->current_method, $content_md5, $content_type, $date, $path));
$sign_string_as_bytes = utf8_encode($sign_string);
$secret_as_bytes = utf8_encode($this->secret_key);
$sign = hash_hmac('sha1', $sign_string_as_bytes, $secret_as_bytes);
$headers = array(sprintf('Host: %s', $this->api_host), sprintf('Authorization: Uploadcare %s:%s', $this->public_key, $sign), sprintf('Date: %s', $date), sprintf('Content-Type: %s', $content_type), sprintf('Content-Length: %d', $content_length), sprintf('Accept: application/vnd.uploadcare-v%s+json', $this->api_version), sprintf('User-Agent: %s', $this->getUserAgent())) + $add_headers;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
}