Kaynağa Gözat

Added PUT and DELETE method

Jan Oris 9 yıl önce
ebeveyn
işleme
712f3f7121
3 değiştirilmiş dosya ile 100 ekleme ve 27 silme
  1. 52 3
      README.md
  2. 1 1
      composer.json
  3. 47 23
      src/Ixudra/Curl/Builder.php

+ 52 - 3
README.md

@@ -155,7 +155,34 @@ Post requests work similar to `GET` requests, but use the `post()` method instea
 
 ```
 
-`PUT` and `DELETE` will be added in the near future.
+
+### Sending PUT requests
+
+Put requests work similar to `POST` requests, but use the `put()` method instead:
+
+```php
+
+    // Send a PUT 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()
+        ->put();
+
+```
+
+
+### Sending DELETE requests
+
+Delete requests work similar to `GET` requests, but use the `delete()` method instead:
+
+```php
+
+    // Send a DELETE request to: http://www.foo.com/bar/1 using JSON
+    $response = Curl::to('http://www.foo.com/bar/1')
+        ->asJson()
+        ->delete();
+
+```
 
 
 ### Downloading files
@@ -172,6 +199,21 @@ For downloading a file, you can use the `download()` method:
 ```
 
 
+### Debugging requests
+
+In case a request fails, it might be useful to get debug the request. In this case, you can use the `debug()` method. 
+This method uses one parameter, which is the name of the file in which the debug information is to be stored:
+
+```php
+
+    // Send a GET request to http://www.foo.com/bar and log debug information in /path/to/dir/logFile.txt
+    $response = Curl::to('http://www.foo.com/bar')
+            ->enableDebug('/path/to/dir/logFile.txt');
+            ->get();
+
+```
+
+
 ### Using cURL options
 
 You can add various cURL options to the request using of several utility methods such as `withHeader()` for adding a 
@@ -194,11 +236,19 @@ use the facades to access the `CurlService`.
     // Send a GET request to: http://www.foo.com/bar
     $response = $curlService->to('http://www.foo.com/bar')
         ->get();
-        
+
     // Send a POST request to: http://www.foo.com/bar
     $response = $curlService->to('http://www.foo.com/bar')
         ->post();
 
+    // Send a PUT request to: http://www.foo.com/bar
+    $response = $curlService->to('http://www.foo.com/bar')
+        ->put();
+
+    // Send a DELETE request to: http://www.foo.com/bar
+    $response = $curlService->to('http://www.foo.com/bar')
+        ->delete();
+
 ```
 
 
@@ -206,7 +256,6 @@ use the facades to access the `CurlService`.
 
 ## Planning
 
- - Add `PUT` and `DELETE` method
  - Add additional utility methods for other cURL options
  - Add contract to allow different HTTP providers such as Guzzle
 

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

+ 47 - 23
src/Ixudra/Curl/Builder.php

@@ -24,6 +24,8 @@ class Builder {
         'data'                  => array(),
         'asJson'                => false,
         'returnAsArray'         => false,
+        'enableDebug'           => false,
+        'debugFile'             => '',
         'saveFile'              => '',
     );
 
@@ -148,6 +150,19 @@ class Builder {
             ->withHeader( 'Connection: Keep-Alive' );
     }
 
+    /**
+     * Enable debug mode for the cURL request
+     *
+     * @param   string $logFile    The full path to the log file you want to use
+     * @return Builder
+     */
+    public function enableDebug($logFile)
+    {
+        return $this->withPackageOption( 'enableDebug', true )
+            ->withPackageOption( 'debugFile', $logFile )
+            ->withOption('VERBOSE', true);
+    }
+
     /**
      * Send a GET request to a URL using the specified cURL options
      *
@@ -205,29 +220,29 @@ class Builder {
         $this->curlOptions[ 'POSTFIELDS' ] = $parameters;
     }
 
-//    /**
-//     * Send a PUT request to a URL using the specified cURL options
-//     *
-//     * @return mixed
-//     */
-//    public function put()
-//    {
-//        $this->setPostParameters();
-//
-//        return $this->send();
-//    }
-//
-//    /**
-//     * Send a DELETE request to a URL using the specified cURL options
-//     *
-//     * @return mixed
-//     */
-//    public function delete()
-//    {
-//        $this->setPostParameters();
-//
-//        return $this->send();
-//    }
+    /**
+     * Send a PUT request to a URL using the specified cURL options
+     *
+     * @return mixed
+     */
+    public function put()
+    {
+        $this->setPostParameters();
+
+        return $this->withOption('CUSTOMREQUEST', 'PUT')
+            ->send();
+    }
+
+    /**
+     * Send a DELETE request to a URL using the specified cURL options
+     *
+     * @return mixed
+     */
+    public function delete()
+    {
+        return $this->withOption('CUSTOMREQUEST', 'DELETE')
+            ->send();
+    }
 
     /**
      * Send the request
@@ -241,6 +256,11 @@ class Builder {
             $this->withHeader( 'Content-Type: application/json' );
         }
 
+        if( $this->packageOptions[ 'enableDebug' ] ) {
+            $debugFile = fopen( $this->packageOptions[ 'debugFile' ], 'w');
+            $this->withOption('STDERR', $debugFile);
+        }
+
         // Create the request with all specified options
         $this->curlObject = curl_init();
         $options = $this->forgeOptions();
@@ -260,6 +280,10 @@ class Builder {
             $response = json_decode($response, $this->packageOptions[ 'returnAsArray' ]);
         }
 
+        if( $this->packageOptions[ 'enableDebug' ] ) {
+            fclose( $debugFile );
+        }
+
         // Return the result
         return $response;
     }