-
Notifications
You must be signed in to change notification settings - Fork 7
/
http_api.php
35 lines (29 loc) · 1.06 KB
/
http_api.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
<?php
use WordPress\AsyncHttp\Client;
use WordPress\AsyncHttp\ClientEvent;
use WordPress\AsyncHttp\Request;
require __DIR__ . '/vendor/autoload.php';
$requests = [
new Request( "https://wordpress.org/latest.zip" ),
new Request( "https://raw.githubusercontent.com/wpaccessibility/a11y-theme-unit-test/master/a11y-theme-unit-test-data.xml" ),
];
$client = new Client();
$client->enqueue( $requests );
while ( $client->await_next_event() ) {
$request = $client->get_request();
echo "Request " . $request->id . ": " . $client->get_event() . " ";
switch ( $client->get_event() ) {
case Client::EVENT_BODY_CHUNK_AVAILABLE:
echo $request->response->received_bytes . "/". $request->response->total_bytes ." bytes received";
file_put_contents( 'downloads/' . $request->id, $client->get_response_body_chunk(), FILE_APPEND);
break;
case Client::EVENT_REDIRECT:
case Client::EVENT_GOT_HEADERS:
case Client::EVENT_FINISHED:
break;
case Client::EVENT_FAILED:
echo "– ❌ Failed request to " . $request->url . " – " . $request->error;
break;
}
echo "\n";
}