PHP code example of amphp / dns

1. Go to this page and download the library: Download amphp/dns library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

amphp / dns example snippets


// Example without type restriction. Will return IPv4 and / or IPv6 addresses.
// What's returned depends on what's available for the given hostname.

/** @var Amp\Dns\DnsRecord[] $records */
$records = Amp\Dns\resolve("github.com");

// Example with type restriction. Will throw an exception if there are no A records.

/** @var Amp\Dns\DnsRecord[] $records */
$records = Amp\Dns\resolve("github.com", Amp\Dns\DnsRecord::A);

/** @var Amp\Dns\DnsRecord[] $records */
$records = Amp\Dns\query("google.com", Amp\Dns\DnsRecord::MX);

/** @var Amp\Dns\DnsRecord[] $records */
$records = Amp\Dns\query("8.8.8.8", Amp\Dns\DnsRecord::PTR);

EventLoop::repeat(600, function () use ($resolver) {
    Amp\Dns\dnsResolver()->reloadConfig();
});



ubIpv4 = Amp\Dns\resolve("github.com", Dns\Record::A);
pretty_print_records("github.com", $githubIpv4);

$firstGoogleResult = Amp\Future\awaitFirst([
  Amp\async(fn() => Amp\Dns\resolve("google.com", Dns\Record::A)),
  Amp\async(fn() => Amp\Dns\resolve("google.com", Dns\Record::AAAA)),
]);

pretty_print_records("google.com", $firstGoogleResult);

$combinedGoogleResult = Amp\Dns\resolve("google.com");
pretty_print_records("google.com", $combinedGoogleResult);

$googleMx = Amp\Dns\query("google.com", Amp\Dns\DnsRecord::MX);
pretty_print_records("google.com", $googleMx);