/
002_custom_request.php
36 lines (29 loc) · 1.15 KB
/
002_custom_request.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
34
35
36
<?php
require __DIR__ . '/../vendor/autoload.php';
try {
// Instantiate the HTTP client
$client = new Amp\Artax\Client;
// Let's build up a custom Request object
$request = (new Amp\Artax\Request)
->setMethod('GET') // <-- defaults to GET if not assigned, so this isn't strictly necessary
->setUri('http://httpbin.org/user-agent')
->setHeader('X-My-Header', 'some-value')
;
// Make an asynchronous HTTP request
$promise = $client->request($request);
// Client::request() is asynchronous! It doesn't return a response. Instead, it
// returns a promise to resolve the response at some point in the future when
// it's finished. Here we tell the promise that we want to wait for it to complete.
$response = $promise->wait();
// Output the results
printf(
"\nHTTP/%s %d %s\n",
$response->getProtocol(),
$response->getStatus(),
$response->getReason()
);
} catch (Amp\Artax\ClientException $e) {
// If something goes wrong the Promise::wait() call will throw the relevant
// exception. The Client::request() method itself will never throw.
echo $e;
}