forked from recoilphp/recoil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns-react
executable file
·50 lines (42 loc) · 1.25 KB
/
dns-react
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
49
50
#!/usr/bin/env php
<?php
/**
* This example implements the same functionality as the DNS example without
* using coroutines (i.e., "vanilla" React).
*/
require dirname(__DIR__) . '/vendor/autoload.php';
/**
* 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)
{
return $resolver
->resolve($name)
->then(
function ($ip) use ($name) {
echo 'Resolved "' . $name . '" to ' . $ip . PHP_EOL;
},
function ($e) use ($name) {
echo 'Failed to resolve "' . $name . '" - ' . $e->getMessage() . PHP_EOL;
}
);
}
// -----------------------------------------------------------------------------
$loop = \React\EventLoop\Factory::create();
$resolver = (new React\Dns\Resolver\Factory)->create(
'8.8.8.8',
$loop
);
\React\Promise\all([
resolveDomainName('recoil.io', $resolver),
resolveDomainName('reactphp.org', $resolver),
resolveDomainName('probably-wont-resolve', $resolver),
])->then(
function () {
echo 'Done' . PHP_EOL;
}
);
$loop->run();