Jelajahi Sumber

Enable File Sending.

Thedigit 8 tahun lalu
induk
melakukan
4c29bc4e25
2 mengubah file dengan 47 tambahan dan 7 penghapusan
  1. 18 6
      README.md
  2. 29 1
      src/Ixudra/Curl/Builder.php

+ 18 - 6
README.md

@@ -97,7 +97,7 @@ Create a new instance of the `CurlService` where you would like to use the packa
 
 ### Laravel usage
 
-The package provides an easy interface for sending cURL requests from your application. The package provides a fluent 
+The package provides an easy interface for sending cURL requests from your application. The package provides a fluent
 interface similar the Laravel query builder to easily configure the request. There are several utility methods that allow
 you to easily add certain options to the request. If no utility method applies, you can also use the general `withOption`
 method.
@@ -183,7 +183,19 @@ 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:
+
+```php
+
+    $response = Curl::to('http://foo.com/bar.png')
+        ->withContentType('multipart/form-data')
+        ->withData( array( 'foz' => 'baz' ) )
+        ->containsFile( true )
+        ->post();
+
+```
 
 ### Downloading files
 
@@ -201,7 +213,7 @@ 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. 
+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
@@ -216,9 +228,9 @@ This method uses one parameter, which is the name of the file in which the debug
 
 ### Using cURL options
 
-You can add various cURL options to the request using of several utility methods such as `withHeader()` for adding a 
-header to the request, or use the general `withOption()` method if no utility method applies. The package will 
-automatically prepend the options with the `CURLOPT_` prefix. It is worth noting that the package does not perform 
+You can add various cURL options to the request using of several utility methods such as `withHeader()` for adding a
+header to the request, or use the general `withOption()` method if no utility method applies. The package will
+automatically prepend the options with the `CURLOPT_` prefix. It is worth noting that the package does not perform
 any validation on the cURL options. Additional information about available cURL options can be found
 [here](http://php.net/manual/en/function.curl-setopt.php).
 
@@ -226,7 +238,7 @@ any validation on the cURL options. Additional information about available cURL
 
 ### Usage without Laravel
 
-Usage without Laravel is identical to usage described previously. The only difference is that you will not be able to 
+Usage without Laravel is identical to usage described previously. The only difference is that you will not be able to
 use the facades to access the `CurlService`.
 
 ```php

+ 29 - 1
src/Ixudra/Curl/Builder.php

@@ -29,6 +29,7 @@ class Builder {
         'returnAsArray'         => false,
         'responseObject'        => false,
         'enableDebug'           => false,
+        'containsFile'          => false,
         'debugFile'             => '',
         'saveFile'              => '',
     );
@@ -209,6 +210,16 @@ class Builder {
             ->withOption('VERBOSE', true);
     }
 
+    /**
+     * Enable File sending
+     *
+     * @return Builder
+     */
+    public function containsFile()
+    {
+        return $this->withPackageOption( 'containsFile', true );
+    }
+
     /**
      * Send a GET request to a URL using the specified cURL options
      *
@@ -309,7 +320,11 @@ class Builder {
 
         // Create the request with all specified options
         $this->curlObject = curl_init();
-        $options = $this->forgeOptions();
+        if($this->packageOptions[ 'containsFile' ]) {
+            $options = $this->forgeFileOptions();
+        }else {
+            $options = $this->forgeOptions();
+        }
         curl_setopt_array( $this->curlObject, $options );
 
         // Send the request
@@ -379,5 +394,18 @@ class Builder {
 
         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;
+    }
 
 }