Examples

Basic Query Syntax

Get the MX Records for google.com

//
// create new resolver object, passing in an array of name
// servers to use for lookups
//
$r = new Net_DNS2_Resolver(array('nameservers' => array('192.168.0.1')));

//
// execute the query request for the google.com MX servers
//
try {
        $result = $r->query('google.com', 'MX');
        
} catch(Net_DNS2_Exception $e) {
        
        echo "::query() failed: ", $e->getMessage(), "\n";
}

//
// loop through the answer, printing out the MX servers retured.
//
foreach($result->answer as $mxrr)
{
        printf("preference=%d, host=%s\n", $mxrr->preference, $mxrr->exchange);
}

Get the OpenPGPKey for an Email Address

//
// the email address to lookup
//
$email = 'hugh@example.com';

//
// get the local part and domain
//
list($local_part, $domain) = explode('@', strtolower($email));

//
// make sure the local part is UTF8
//
$local_part = utf8_encode($local_part);

//
// get the sha256 hash of the local part
//
$local_part_hash = hash('sha256', $local_part);

//
// RFC 7929 says the hash should be truncated to 28 octects, but before it's
// hex encoded; since hex is base-16, we're looking for 56 octects off the
// hex encoded hash.
//
$local_part_hash = substr($local_part_hash, 0, 56);

//
// build the host portion; this ends up looking like:
//
// c93f1e400f26708f98cb19d936620da35eec8f72e57f9eec01c1afd6._openpgpkey.example.com
//
$host = $local_part_hash . '._openpgpkey.' . $domain;

//
// do the lookup
//
$r = new Net_DNS2_Resolver(array('nameservers' => array('192.168.0.1')));

//
// Enable DNSSEC
//
$r->dnssec = true;

try {
        $result = $r->query($host, 'OPENPGPKEY');

        foreach($result->answer as $index => $o)
        {
                if ($o instanceof Net_DNS2_RR_OPENPGPKEY)
                {
                        echo 'The OpenPGPKey is: ' . $o->key . "\n";
                }
        }

} catch(Net_DNS2_Exception $e)
{
        echo $e->getMessage()."\n";
}

Zone Transfer (AXFR) for example.com

//
// create new resolver object, passing in an array of name
// servers to use for lookups
//
$r = new Net_DNS2_Resolver(array('nameservers' => array('192.168.0.1')));

//
// add a TSIG to authenticate the request
//
$r->signTSIG('mykey', '9dnf93asdf39fs');

//
// execute the query request for the google.com MX servers
//
try {
        $result = $r->query('example.com', 'AXFR');
        
} catch(Net_DNS2_Exception $e) {
        
        echo "::query() failed: ", $e->getMessage(), "\n";
}

//
// loop through the answer, printing out each resource record.
//
foreach($result->answer as $rr)
{
        echo $rr;
}

Basic Update Syntax

//
// create a new Updater object
//
$u = new Net_DNS2_Updater('example.com', array('nameservers' => array('192.168.0.1')));

try {
        //
        // create a new MX RR object to add to the example.com zone
        //
        $mx = Net_DNS2_RR::fromString('test.example.com MX 10 mail.google.com');

        //
        // add the record
        //
        $u->add($mx);

        //
        // add a TSIG to authenticate the request
        //
        $u->signTSIG('my-key', '9dnf93asdf39fs');

        //
        // execute the request
        //
        $u->update();
        
} catch(Net_DNS2_Exception $e) {

        echo "::update() failed: ", $e->getMessage(), "\n";
}