Browse Source

Added key-value support for withHeaders() method

Jan Oris 5 years ago
parent
commit
60f1e448c0
4 changed files with 34 additions and 2 deletions
  1. 4 0
      CHANGELOG.md
  2. 18 0
      README.md
  3. 1 1
      composer.json
  4. 11 1
      src/Builder.php

+ 4 - 0
CHANGELOG.md

@@ -2,6 +2,10 @@
 
 All Notable changes to `ixudra/curl` will be documented in this file
 
+## 6.18.0 - 2019-11-01
+### Added
+- Added key-value support to `withHeaders()` method
+
 ## 6.17.0 - 2019-09-13
 ### Added
 - Added support for multiple response headers with the same name

+ 18 - 0
README.md

@@ -6,6 +6,9 @@ ixudra/curl
 [![StyleCI](https://styleci.io/repos/18486198/shield)](https://styleci.io/repos/18486198)
 [![Total Downloads](https://img.shields.io/packagist/dt/ixudra/curl.svg?style=flat-square)](https://packagist.org/packages/ixudra/curl)
 
+
+![Ixudra Curl](https://repository-images.githubusercontent.com/18486198/cd2b2080-de01-11e9-8fb1-e64ffe5e9816)
+
 Custom PHP cURL library for the Laravel 4 or 5 framework - developed by [Ixudra](http://ixudra.be).
 
 The package provides an easy interface for sending cURL requests from your PHP web application. The package provides an 
@@ -298,6 +301,21 @@ Alternatively, you can use the `withHeaders()` to combine multiple headers into
 
 ```
 
+You can also use key-value when using the `withHeaders()` method:
+
+```php
+
+    use Ixudra\Curl\Facades\Curl;
+
+    // Send a GET request to: http://www.foo.com/bar with 2 custom headers
+    $response = Curl::to('http://foo.com/bar')
+        ->withHeaders( array( 'MyFirstHeader' => '123', 'MySecondHeader' => '456' ) )
+        ->get();
+
+```
+
+ > Note that headers will override each other if you add the same header more than once.
+
 
 ### Specifying the content type
 

+ 1 - 1
composer.json

@@ -1,7 +1,7 @@
 {
     "name": "ixudra/curl",
     "description": "Custom PHP Curl library for the Laravel 5 framework - developed by Ixudra",
-    "version": "6.17.0",
+    "version": "6.18.0",
     "keywords": ["Ixudra", "Laravel", "Curl"],
     "homepage": "http://ixudra.be",
     "license": "MIT",

+ 11 - 1
src/Builder.php

@@ -233,8 +233,17 @@ class Builder {
      */
     public function withHeaders(array $headers)
     {
+        $data = array();
+        foreach( $headers as $key => $value ) {
+            if( !is_numeric($key) ) {
+                $value = $key .': '. $value;
+            }
+
+            $data[] = $value;
+        }
+
         $this->curlOptions[ 'HTTPHEADER' ] = array_merge(
-            $this->curlOptions[ 'HTTPHEADER' ], $headers
+            $this->curlOptions[ 'HTTPHEADER' ], $data
         );
 
         return $this;
@@ -480,6 +489,7 @@ class Builder {
         // Create the request with all specified options
         $this->curlObject = curl_init();
         $options = $this->forgeOptions();
+        dd( $options );
         curl_setopt_array( $this->curlObject, $options );
 
         // Send the request