1
0
mirror of https://github.com/danog/dns.git synced 2025-01-23 05:51:11 +01:00
dns/README.md
2014-09-24 00:06:42 -04:00

1.2 KiB

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