-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
4-forms.php
33 lines (25 loc) · 997 Bytes
/
4-forms.php
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
<?php declare(strict_types=1);
use Amp\Http\Client\Form;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
require __DIR__ . '/../.helper/functions.php';
try {
// Instantiate the HTTP client
$client = HttpClientBuilder::buildDefault();
// Here we create a custom request object instead of simply passing an URL to request().
// We set the method to POST and add a FormBody to submit a form.
$body = new Form;
$body->addField("search", "foobar");
$body->addField("submit", "ok");
$body->addFile("foo", __DIR__ . "/small-file.txt");
$request = new Request('https://httpbin.org/post', 'POST');
$request->setBody($body);
// Make an asynchronous HTTP request
$response = $client->request($request);
dumpRequestTrace($response->getRequest());
dumpResponseTrace($response);
dumpResponseBodyPreview($response->getBody()->buffer());
} catch (HttpException $error) {
echo $error;
}