@@ -109,6 +109,12 @@ class Api
109109 */
110110 private $ options ;
111111
112+ /**
113+ * internal customHeaders array
114+ * @var array
115+ */
116+ private $ customHeaders ;
117+
112118
113119 /**
114120 * Create an L{API} object.
@@ -121,6 +127,8 @@ public function __construct($user_key, $service_url = 'https://api.rosette.com/
121127 {
122128 $ this ->user_key = $ user_key ;
123129
130+
131+
124132 $ this ->headers = array ("X-RosetteAPI-Key: $ user_key " ,
125133 "Content-Type: application/json " ,
126134 "Accept: application/json " ,
@@ -129,6 +137,14 @@ public function __construct($user_key, $service_url = 'https://api.rosette.com/
129137 "X-RosetteAPI-Binding: php " ,
130138 "X-RosetteAPI-Binding-Version: " . self ::$ binding_version );
131139
140+ /*$this->headers = $this->addHeaders(array("X-RosetteAPI-Key: $user_key",
141+ "Content-Type: application/json",
142+ "Accept: application/json",
143+ "Accept-Encoding: gzip",
144+ "User-Agent: RosetteAPIPHP/" . self::$binding_version,
145+ "X-RosetteAPI-Binding: php",
146+ "X-RosetteAPI-Binding-Version: " . self::$binding_version ));*/
147+
132148 $ this ->setServiceUrl ($ service_url );
133149 $ this ->setDebug (false );
134150 $ this ->setTimeout (300 );
@@ -301,6 +317,72 @@ public function clearOptions()
301317 $ this ->options = array ();
302318 }
303319
320+ /**
321+ * Setter for options
322+ *
323+ *
324+ * @return array
325+ */
326+ public function getCustomHeaders ()
327+ {
328+ /*if (sizeof($this->customHeaders) > 0) {
329+ return $this->customHeaders;
330+ } else {
331+ return null;
332+ }*/
333+ return $ this ->customHeaders ;
334+ }
335+
336+ /**
337+ * Setter for custom headers
338+ *
339+ * @param array $headers
340+ *
341+ */
342+ public function setCustomHeaders ($ headers )
343+ {
344+ $ this ->clearCustomHeaders ();
345+ if ($ headers != null ){
346+ foreach ($ headers as $ key => $ value ) {
347+ if (preg_match ("/^X-RosetteAPI-/ " , $ value )){
348+ array_push ($ this ->customHeaders , $ value );
349+ } else {
350+ throw new RosetteException ("Custom headers must start with \"X- \"" );
351+ }
352+ }
353+ }
354+ }
355+
356+ /**
357+ * Adds custom headers to headers array if there are any
358+ *
359+ * @param array $headers
360+ *
361+ * @return array $headers
362+ **/
363+ private function addHeaders ($ headers )
364+ {
365+ $ customHeaders = $ this ->getCustomHeaders ();
366+
367+ if (sizeof ($ customHeaders ) > 0 ){
368+ foreach ($ customHeaders as $ value )
369+ {
370+ array_push ($ headers , $ value );
371+ }
372+ return $ headers ;
373+ } else {
374+ return $ headers ;
375+ }
376+ }
377+
378+ /**
379+ * Clears all custom headers
380+ */
381+ public function clearCustomHeaders ()
382+ {
383+ $ this ->customHeaders = array ();
384+ }
385+
304386
305387 /**
306388 * Replaces a header item with a new one
@@ -331,16 +413,7 @@ private function callEndpoint($parameters, $subUrl)
331413 $ this ->subUrl = $ subUrl ;
332414 $ resultObject = '' ;
333415
334- // if custom headers exist, add them
335- if ($ parameters ->customHeaders != null ){
336- foreach ($ parameters ->customHeaders as $ key => $ value ) {
337- if (strpos ($ value , "X " ) === 0 && strpos ($ value , "- " ) === 1 ){
338- array_push ($ this ->headers , $ value );
339- } else {
340- throw new RosetteException ("Custom headers must start with \"X- \"" );
341- }
342- }
343- }
416+ $ this ->headers = $ this ->addHeaders ($ this ->headers );
344417
345418 if (strlen ($ parameters ->getMultiPartContent ()) > 0 ) {
346419 $ content = $ parameters ->getMultiPartContent ();
0 commit comments