-
Notifications
You must be signed in to change notification settings - Fork 38
/
dns
executable file
·48 lines (39 loc) · 1.26 KB
/
dns
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
44
45
46
47
48
#!/usr/bin/env php
<?php
/**
* This is the executable example of the DNS example given in README.md.
*/
declare(strict_types = 1);
require __DIR__ . '/../vendor/autoload.php';
use Recoil\React\ReactKernel;
use Recoil\Recoil;
/**
* Resolve a name to an IP address.
*
* @param string $name The name to resolve.
* @param React\Dns\Resolver\Resolver $resolver The resolver to use.
*/
function resolveDomainName($name, React\Dns\Resolver\Resolver $resolver)
{
try {
$ip = yield $resolver->resolve($name);
echo 'Resolved "' . $name . '" to ' . $ip . PHP_EOL;
} catch (Exception $e) {
echo 'Failed to resolve "' . $name . '" - ' . $e->getMessage() . PHP_EOL;
}
}
// -----------------------------------------------------------------------------
ReactKernel::start(function () {
$resolver = (new React\Dns\Resolver\Factory)->create(
'8.8.8.8',
yield Recoil::eventLoop()
);
// Yielding an array of coroutines executes them in parallel and waits
// until all have returned a value.
yield [
resolveDomainName('recoil.io', $resolver),
resolveDomainName('php.net', $resolver),
resolveDomainName('probably-wont-resolve', $resolver),
];
echo 'Done.' . PHP_EOL;
});