supporting curl_file_create`
This commit is contained in:
@@ -71,11 +71,13 @@ 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:
|
||||||
```php
|
```php
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user