-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhttp-example.php
43 lines (35 loc) · 1.4 KB
/
http-example.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
37
38
39
40
41
42
43
<?php
require_once __DIR__ . "/src/Async.php";
require_once __DIR__ . "/src/Http/Http.php";
use Http\Http;
header("content-type: text/plain");
// Top-level Fiber (equivalent to top-level async/await in JavaScript)
$main = new Fiber(function(){
$requests = [
new Http("get", "https://animetavern.com"),
new Http("get", "https://footbridgemedia.com"),
new Http("get", "https://www.mcmahonplumbing.com/"),
new Http("get", "https://www.prowaterheatersnj.com/"),
new Http("get", "https://codeburst.io/top-10-discord-servers-for-developers-86570fcdbff3"),
new Http("get", "https://www.php.net/manual/en/stream.errors.php"),
new Http("get", "https://discord.me/devcord"),
];
$startTime = microtime(true);
foreach($requests as $request){
// Async function. All awaits are non-blocking
$childFiber = new Fiber(function() use ($request, &$finishedLoops){
Async::await($request->connect());
print(sprintf("Connected %s\n", $request->host));
$response = Async::await($request->fetch());
print(sprintf("Finished %s\n", $request->host));
});
$childFiber->start();
}
// Start the event loop of all available fibers. This is blocking
// TODO Make this yield as well!
Async::run();
// Microtime is seconds on Windows as a float
printf("All requests finished asynchronously in %fs\n", microtime(true) - $startTime);
});
// Start the top-level Fiber
$main->start();