@@ -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 " ,
@@ -301,6 +309,65 @@ public function clearOptions()
301309 $ this ->options = array ();
302310 }
303311
312+ /**
313+ * Setter for options
314+ *
315+ *
316+ * @return array
317+ */
318+ public function getCustomHeaders ()
319+ {
320+ return $ this ->customHeaders ;
321+ }
322+
323+ /**
324+ * Setter for custom headers
325+ *
326+ * @param array $headers
327+ *
328+ */
329+ public function setCustomHeaders ($ header )
330+ {
331+ $ this ->clearCustomHeaders ();
332+ if ($ header != null ){
333+ if (preg_match ("/^X-RosetteAPI-/ " , $ header )){
334+ array_push ($ this ->customHeaders , $ header );
335+ } else {
336+ throw new RosetteException ("Custom headers must start with \"X- \"" );
337+ }
338+ }
339+ }
340+
341+ /**
342+ * Adds custom headers to headers array if there are any
343+ *
344+ * @param array $headers
345+ *
346+ * @return array $headers
347+ **/
348+ private function addHeaders ($ headers )
349+ {
350+ $ customHeaders = $ this ->getCustomHeaders ();
351+
352+ if (sizeof ($ customHeaders ) > 0 ){
353+ foreach ($ customHeaders as $ value )
354+ {
355+ array_push ($ headers , $ value );
356+ }
357+ return $ headers ;
358+ } else {
359+ return $ headers ;
360+ }
361+ }
362+
363+ /**
364+ * Clears all custom headers
365+ */
366+ public function clearCustomHeaders ()
367+ {
368+ $ this ->customHeaders = array ();
369+ }
370+
304371
305372 /**
306373 * Replaces a header item with a new one
@@ -331,6 +398,8 @@ private function callEndpoint($parameters, $subUrl)
331398 $ this ->subUrl = $ subUrl ;
332399 $ resultObject = '' ;
333400
401+ $ this ->headers = $ this ->addHeaders ($ this ->headers );
402+
334403 if (strlen ($ parameters ->getMultiPartContent ()) > 0 ) {
335404 $ content = $ parameters ->getMultiPartContent ();
336405 $ filename = $ parameters ->fileName ;
@@ -357,6 +426,7 @@ private function callEndpoint($parameters, $subUrl)
357426
358427 $ resultObject = $ this ->postHttp ($ url , $ this ->headers , $ multi );
359428 } else {
429+
360430 $ url = $ this ->service_url . $ this ->subUrl ;
361431 $ resultObject = $ this ->postHttp ($ url , $ this ->headers , $ parameters ->serialize ($ this ->options ));
362432 }
0 commit comments