Browse Source

Return response headers in response objects

Jan Oris 7 years ago
parent
commit
0c60efac63
3 changed files with 72 additions and 23 deletions
  1. 4 0
      CHANGELOG.md
  2. 36 0
      README.md
  3. 32 23
      src/Builder.php

+ 4 - 0
CHANGELOG.md

@@ -2,6 +2,10 @@
 
 
 All Notable changes to `ixudra/curl` will be documented in this file
 All Notable changes to `ixudra/curl` will be documented in this file
 
 
+## 6.16.0 - 2017-12-07
+### Added
+- Added method to return response headers in the response objects
+
 ## 6.15.1 - 2017-11-07
 ## 6.15.1 - 2017-11-07
 ### Added
 ### Added
 - Bugfix: wrong variable used
 - Bugfix: wrong variable used

+ 36 - 0
README.md

@@ -408,6 +408,42 @@ The response object will look like this:
 }
 }
 ```
 ```
 
 
+### Response headers
+
+In some cases it might be relevant to return the response headers back to the user. This can easily be done using the `withResponseHeaders()` method.
+
+```php
+
+    use Ixudra\Curl\Facades\Curl;
+
+    // Send a GET request to http://www.foo.com/bar and return a response object with additional information including response headers
+    $response = Curl::to('http://www.foo.com/bar')
+        ->withResponseHeaders()
+        ->returnResponseObject()
+        ->get();
+            
+    $content = $response->content;
+    $headers = $response->headers;
+
+```
+
+The response object will look like this:
+
+```json
+{
+    "content": "Message content here",
+    "status": 200,
+    "contentType": "content-type response header (ex: application/json)",
+    "error": "Error message goes here (Only added if an error occurs)",
+    "headers": {
+        "header-type-1": "header-content-1",
+        "header-type-2": "header-content-2"
+    }
+}
+```
+
+It is important to note that the `withResponseHeaders()` method must be used in conjunction with the `returnResponseObject()` method in order to see the returned headers
+
 
 
 ### Debugging requests
 ### Debugging requests
 
 

+ 32 - 23
src/Builder.php

@@ -252,6 +252,16 @@ class Builder {
             ->withHeader( 'Connection: Keep-Alive' );
             ->withHeader( 'Connection: Keep-Alive' );
     }
     }
 
 
+    /**
+     * Add response headers to the response object or response array
+     *
+     * @return Builder
+     */
+    public function withResponseHeaders()
+    {
+        return $this->withCurlOption( 'HEADER', TRUE );
+    }
+
     /**
     /**
      * Return a full response object with HTTP status and headers instead of only the content
      * Return a full response object with HTTP status and headers instead of only the content
      *
      *
@@ -475,11 +485,11 @@ class Builder {
         // Send the request
         // Send the request
         $response = curl_exec( $this->curlObject );
         $response = curl_exec( $this->curlObject );
 
 
-        $response_header = null;
-        if( $this->curlOptions[ 'HEADER' ]) {
-            $header_size = curl_getinfo( $this->curlObject, CURLINFO_HEADER_SIZE );
-            $response_header = substr( $response, 0, $header_size );
-            $response = substr( $response, $header_size );
+        $responseHeader = null;
+        if( $this->curlOptions[ 'HEADER' ] ) {
+            $headerSize = curl_getinfo( $this->curlObject, CURLINFO_HEADER_SIZE );
+            $responseHeader = substr( $response, 0, $headerSize );
+            $response = substr( $response, $headerSize );
         }
         }
 
 
         // Capture additional request information if needed
         // Capture additional request information if needed
@@ -509,26 +519,32 @@ class Builder {
         }
         }
 
 
         // Return the result
         // Return the result
-        return $this->returnResponse( $response_header, $response, $responseData );
+        return $this->returnResponse( $response, $responseData, $responseHeader );
     }
     }
 
 
-    protected function parseHeader($response_header) {
-        $res = array_filter( array_map( function( $x ) {
-            $arr = array_map( "trim", explode( ":", $x, 2 ) );
-            if( sizeof($arr) == 2 ) {
-                return [$arr[0] => $arr[1]];
+    /**
+     * @param   string $headerString    Response header string
+     * @return mixed
+     */
+    protected function parseHeaders($headerString)
+    {
+        $headers = array_filter(array_map(function ($x) {
+            $arr = array_map('trim', explode(':', $x, 2));
+            if( count($arr) == 2 ) {
+                return [ $arr[ 0 ] => $arr[ 1 ] ];
             }
             }
-        }, array_filter( array_map( "trim", explode( "\r\n", $response_header ) ) ) ) );
+        }, array_filter(array_map('trim', explode("\r\n", $headerString)))));
 
 
-        return array_collapse($res);
+        return array_collapse($headers);
     }
     }
 
 
     /**
     /**
      * @param   mixed $content          Content of the request
      * @param   mixed $content          Content of the request
      * @param   array $responseData     Additional response information
      * @param   array $responseData     Additional response information
+     * @param   string $header          Response header string
      * @return mixed
      * @return mixed
      */
      */
-    protected function returnResponse($header, $content, array $responseData = array())
+    protected function returnResponse($content, array $responseData = array(), $header = null)
     {
     {
         if( !$this->packageOptions[ 'responseObject' ] && !$this->packageOptions[ 'responseArray' ] ) {
         if( !$this->packageOptions[ 'responseObject' ] && !$this->packageOptions[ 'responseArray' ] ) {
             return $content;
             return $content;
@@ -541,8 +557,9 @@ class Builder {
         if( array_key_exists('errorMessage', $responseData) ) {
         if( array_key_exists('errorMessage', $responseData) ) {
             $object->error = $responseData[ 'errorMessage' ];
             $object->error = $responseData[ 'errorMessage' ];
         }
         }
+
         if( $this->curlOptions[ 'HEADER' ] ) {
         if( $this->curlOptions[ 'HEADER' ] ) {
-            $object->header = $this->parseHeader( $header );
+            $object->headers = $this->parseHeaders( $header );
         }
         }
 
 
         if( $this->packageOptions[ 'responseObject' ] ) {
         if( $this->packageOptions[ 'responseObject' ] ) {
@@ -597,12 +614,4 @@ class Builder {
         return $this->curlOptions[ 'URL' ] .= $parameterString;
         return $this->curlOptions[ 'URL' ] .= $parameterString;
     }
     }
 
 
-    /**
-     * Get output header
-     *
-     * @return Builder
-     */
-    public function returnResponseHeader() {
-        return $this->withCurlOption( 'HEADER', TRUE );
-    }
 }
 }