supporting curl_file_create`

This commit is contained in:
thefosk
2014-01-08 19:05:22 -08:00
parent bf865a5c30
commit 1504e81ff1
3 changed files with 16 additions and 2 deletions

View File

@@ -71,10 +71,12 @@ To upload files in a multipart form representation simply place an `@` symbol be
```php ```php
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
array( array(
"file" => "@/tmp/file.txt" "file" => Unirest::file("/tmp/file.txt")
) )
); );
``` ```
Use the value of `Unirest::file($path)` to assign a file to a parameter.
### Custom Entity Body ### Custom Entity Body
Sending a custom body such as a JSON Object rather than a string or form style parameters we utilize json_encode for the body: Sending a custom body such as a JSON Object rather than a string or form style parameters we utilize json_encode for the body:

View File

@@ -113,6 +113,18 @@
return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password); return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password);
} }
/**
* Prepares a file for upload. To be used inside the parameters declaration for a request.
* @param string $path The file path
*/
public static function file($path) {
if (function_exists("curl_file_create")) {
return curl_file_create($path);
} else {
return "@" . $path;
}
}
/** /**
* This function is useful for serializing multidimensional arrays, and avoid getting * This function is useful for serializing multidimensional arrays, and avoid getting
* the "Array to string conversion" notice * the "Array to string conversion" notice

View File

@@ -94,7 +94,7 @@ class UnirestTest extends UnitTestCase
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
array( array(
"name" => "Mark", "name" => "Mark",
"file" => "@" . dirname(__FILE__) . "/test_upload.txt" "file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
) )
); );
$this->assertEqual(200, $response->code); $this->assertEqual(200, $response->code);