1
0
mirror of https://github.com/danog/dns.git synced 2024-11-30 04:29:06 +01:00
Go to file
2014-09-24 00:06:42 -04:00
examples Initial amphp refactor 2014-09-24 00:04:46 -04:00
lib/Dns Initial amphp refactor 2014-09-24 00:04:46 -04:00
test/Dns Initial amphp refactor 2014-09-24 00:04:46 -04:00
.gitignore Initial amphp refactor 2014-09-24 00:04:46 -04:00
.travis.yml add PHP 5.6 to .travis.yml 2014-08-29 01:26:26 +09:00
composer.json Initial amphp refactor 2014-09-24 00:04:46 -04:00
phpunit.xml Initial amphp refactor 2014-09-24 00:04:46 -04:00
README.md Improve README code formatting 2014-09-24 00:06:42 -04:00

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();
});