add custom request header #19

Closed
BluesyLong opened this Issue Oct 11, 2012 · 1 comment

Projects

None yet

2 participants

@BluesyLong

I made a little patch to add some headers to the request.
I use it to authenticate with an api key in the header (redmine).

--- ActiveResource.php.orig 2012-10-11 09:40:33.000000000 +0200
+++ ActiveResource.php  2012-10-11 09:40:12.000000000 +0200
@@ -96,6 +96,11 @@
    var $request_body = '';

    /**
+    * The request headers that was sent to the server.
+    */
+   var $request_headers = array();
+
+   /**
     * The complete URL that the request was sent to.
         */
            var $request_uri = '';
@@ -615,7 +620,7 @@
        }

        if ($this->request_format == 'xml') {
-           curl_setopt ($ch, CURLOPT_HTTPHEADER, array ("Expect:", "Content-Type: text/xml", "Length: " .          strlen  $params)));
+           $this->request_headers = array_merge($this->request_headers, array ("Expect:", "Content-Type: text/xml",    Length: " . strlen ($params)));
        }
        switch ($method) {
            case 'POST':
@@ -635,6 +640,10 @@
            default:
                break;
        }
+       if (count($this->request_headers)) {
+           curl_setopt ($ch, CURLOPT_HTTPHEADER, $this->request_headers);
+       }
+
        $res = curl_exec ($ch);

        // Check HTTP status code for denied access
@jbroadway jbroadway closed this in 893247b Oct 11, 2012
@jbroadway
Owner

Awesome, thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment