THRIFT-4072 php: TCurlClient - Add the possibility to send custom headers
Client: php
This closes #1178
diff --git a/lib/php/lib/Thrift/Transport/TCurlClient.php b/lib/php/lib/Thrift/Transport/TCurlClient.php
index 4b3e694..c761cd0 100644
--- a/lib/php/lib/Thrift/Transport/TCurlClient.php
+++ b/lib/php/lib/Thrift/Transport/TCurlClient.php
@@ -84,6 +84,13 @@
protected $timeout_;
/**
+ * http headers
+ *
+ * @var array
+ */
+ protected $headers_;
+
+ /**
* Make a new HTTP client.
*
* @param string $host
@@ -102,6 +109,7 @@
$this->request_ = '';
$this->response_ = null;
$this->timeout_ = null;
+ $this->headers_ = array();
}
/**
@@ -193,9 +201,14 @@
$host = $this->host_.($this->port_ != 80 ? ':'.$this->port_ : '');
$fullUrl = $this->scheme_."://".$host.$this->uri_;
- $headers = array('Accept: application/x-thrift',
- 'Content-Type: application/x-thrift',
- 'Content-Length: '.TStringFuncFactory::create()->strlen($this->request_));
+ $headers = array();
+ $defaultHeaders = array('Accept' => 'application/x-thrift',
+ 'Content-Type' => 'application/x-thrift',
+ 'Content-Length' => TStringFuncFactory::create()->strlen($this->request_));
+ foreach (array_merge($defaultHeaders, $this->headers_) as $key => $value) {
+ $headers[] = "$key: $value";
+ }
+
curl_setopt(self::$curlHandle, CURLOPT_HTTPHEADER, $headers);
if ($this->timeout_ > 0) {
@@ -228,4 +241,9 @@
}
}
+ public function addHeaders($headers)
+ {
+ $this->headers_ = array_merge($this->headers_, $headers);
+ }
+
}