mirror of
https://github.com/danog/dns.git
synced 2024-11-30 04:29:06 +01:00
examples | ||
lib/Dns | ||
test/Dns | ||
.gitignore | ||
.travis.yml | ||
composer.json | ||
phpunit.xml | ||
README.md |
dns
Asynchronous DNS resolution built on the Amp concurrency framework
Examples
Synchronous Wait
<?php
require __DIR__ . '/vendor/autoload.php';
$name = 'google.com';
$resolver = new Amp\Dns\Resolver;
$promise = $resolver->resolve($name);
list($address, $type) = $promise->wait();
printf("%s resolved to %s\n", $name, $address);
Parallel Async
Amp\run(function() {
$names = [
'github.com',
'google.com',
'stackoverflow.com',
'localhost',
'192.168.0.1',
'::1',
];
$promises = [];
$resolver = new Amp\Dns\Resolver;
foreach ($names as $name) {
$promise = $resolver->resolve($name);
$promises[$name] = $promise;
}
// Combine our multiple promises into a single promise
$comboPromise = Amp\some($promises);
// Yield control until the combo promise resolves
list($errors, $successes) = (yield $comboPromise);
foreach ($names as $name) {
echo isset($errors[$name])
? "FAILED: {$name}\n"
: "{$name} => {$successes[$name][0]}\n";
}
// Stop the event loop so we don't sit around forever
Amp\stop();
});