Просмотр исходного кода

Minor refactoring + added PATCH request method

Jan Oris 8 лет назад
Родитель
Сommit
fab3b30c85
3 измененных файлов с 40 добавлено и 26 удалено
  1. 21 3
      README.md
  2. 1 1
      composer.json
  3. 18 22
      src/Ixudra/Curl/Builder.php

+ 21 - 3
README.md

@@ -171,6 +171,21 @@ Put requests work similar to `POST` requests, but use the `put()` method instead
 ```
 
 
+### Sending PATCH requests
+
+Patch requests work similar to `POST` requests, but use the `patch()` method instead:
+
+```php
+
+    // Send a PATCH request to: http://www.foo.com/bar/1 with arguments 'foz' = 'baz' using JSON
+    $response = Curl::to('http://www.foo.com/bar/1')
+        ->withData( array( 'foz' => 'baz' ) )
+        ->asJson()
+        ->patch();
+
+```
+
+
 ### Sending DELETE requests
 
 Delete requests work similar to `GET` requests, but use the `delete()` method instead:
@@ -183,20 +198,23 @@ Delete requests work similar to `GET` requests, but use the `delete()` method in
         ->delete();
 
 ```
-### Sending Files via Curl
 
-For Posting a file, you can use the `containsFile` method to correctly format a request before sending:
+
+### Sending files via Curl
+
+For sending files via a POST request, you can use the `containsFile` method to correctly format a request before sending:
 
 ```php
 
     $response = Curl::to('http://foo.com/bar.png')
         ->withContentType('multipart/form-data')
         ->withData( array( 'foz' => 'baz' ) )
-        ->containsFile( true )
+        ->containsFile()
         ->post();
 
 ```
 
+
 ### Downloading files
 
 For downloading a file, you can use the `download()` method:

+ 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.3.0",
+    "version": "6.4.0",
     "keywords": ["Ixudra", "Laravel", "Curl"],
     "homepage": "http://ixudra.be",
     "license": "MIT",

+ 18 - 22
src/Ixudra/Curl/Builder.php

@@ -290,6 +290,19 @@ class Builder {
             ->send();
     }
 
+    /**
+     * Send a PATCH request to a URL using the specified cURL options
+     *
+     * @return mixed
+     */
+    public function patch()
+    {
+        $this->setPostParameters();
+
+        return $this->withOption('CUSTOMREQUEST', 'PATCH')
+            ->send();
+    }
+
     /**
      * Send a DELETE request to a URL using the specified cURL options
      *
@@ -320,11 +333,7 @@ class Builder {
 
         // Create the request with all specified options
         $this->curlObject = curl_init();
-        if($this->packageOptions[ 'containsFile' ]) {
-            $options = $this->forgeFileOptions();
-        }else {
-            $options = $this->forgeOptions();
-        }
+        $options = $this->forgeOptions();
         curl_setopt_array( $this->curlObject, $options );
 
         // Send the request
@@ -383,29 +392,16 @@ class Builder {
     {
         $results = array();
         foreach( $this->curlOptions as $key => $value ) {
-            $array_key = constant( 'CURLOPT_' . $key );
+            $arrayKey = constant( 'CURLOPT_' . $key );
 
-            if( $key == 'POSTFIELDS' && is_array( $value ) ) {
-                $results[ $array_key ] = http_build_query( $value, null, '&' );
+            if( !$this->packageOptions[ 'containsFile' ] && $key == 'POSTFIELDS' && is_array( $value ) ) {
+                $results[ $arrayKey ] = http_build_query( $value, null, '&' );
             } else {
-                $results[ $array_key ] = $value;
+                $results[ $arrayKey ] = $value;
             }
         }
 
         return $results;
     }
-    /**
-     * Convert the curlOptions with out the 'http_build_query' allowing for posting files.
-     * TODO:: make orignial funtion an overloaded function
-     * @return array
-     */
-    public function forgeFileOptions() {
-        $results = array();
-        foreach( $this->curlOptions as $key => $value ) {
-            $array_key = constant( 'CURLOPT_' . $key );
-            $results[ $array_key ] = $value;
-        }
-        return $results;
-    }
 
 }