-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path05-upload-file.php
More file actions
36 lines (32 loc) · 899 Bytes
/
Copy path05-upload-file.php
File metadata and controls
36 lines (32 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
require(implode(DIRECTORY_SEPARATOR, ["..", "vendor", "autoload.php"]));
use GT\Fetch\Http;
use GT\Http\FormData;
use GT\Http\Response;
use GT\Json\JsonObject;
$formData = new FormData();
$formData->set("upload", new SplFileObject(__FILE__));
$http = new Http();
$http->fetch("https://postman-echo.com/post", [
"method" => "POST",
"headers" => [
"Content-type" => "multipart/form-data"
],
"body" => $formData,
])
->then(function(Response $response) {
if(!$response->ok) {
throw new RuntimeException("Error uploading file to Postman Echo.");
}
return $response->json();
})
->then(function(JsonObject $json) {
foreach($json->asArray()["files"] as $fileName => $data) {
echo $fileName . " - " . strlen($data) . " bytes", PHP_EOL;
}
})
->catch(function(Throwable $error) {
echo "An error occurred: ", $error->getMessage();
});
$http->wait();
die("done waiting");