Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Exporting a DNS Zone file from the TransIP API
<?php
require __DIR__ . '/vendor/autoload.php';
use Badcow\DNS\Zone;
use Badcow\DNS\Rdata\Factory;
use Badcow\DNS\ResourceRecord;
use Badcow\DNS\AlignedBuilder;
// Login details, fill those in with your TransIP username, the file containing
// your private key, and the domain you would like to export
Transip_ApiSettings::$login = "<username>";
Transip_ApiSettings::$privateKey = file_get_contents('<private_key_file>');
$domainName = '<yourdomain.com>';
// Get the domain info
$domain = Transip_DomainService::getInfo($domainName);
// Construct the zone file
$zone = new Zone($domainName . '.');
$zone->setDefaultTtl(1); // 1 means automatic
foreach($domain->dnsEntries as $dnsEntry) {
$rr = new ResourceRecord;
$rr->setName($dnsEntry->name);
switch($dnsEntry->type) {
case Transip_DnsEntry::TYPE_A:
$rr->setRdata(Factory::A($dnsEntry->content));
break;
case Transip_DnsEntry::TYPE_AAAA:
$rr->setRdata(Factory::Aaaa($dnsEntry->content));
break;
case Transip_DnsEntry::TYPE_CNAME:
$rr->setRdata(Factory::Cname($dnsEntry->content));
break;
case Transip_DnsEntry::TYPE_MX:
list($pref,$cont) = explode(' ', $dnsEntry->content);
$rr->setRdata(Factory::Mx($pref, $cont));
break;
case Transip_DnsEntry::TYPE_TXT:
$rr->setRdata(Factory::Txt($dnsEntry->content));
break;
default:
throw new \RuntimeException('Migrating ' . $dnsEntry->type . ' records is not implemented');
}
$zone->addResourceRecord($rr);
}
echo AlignedBuilder::build($zone);
{
"require" : {
"transip/transip-api-php" : "dev-master",
"Badcow/DNS": "^1.0"
},
"repositories": [
{
"type" : "vcs",
"url" : "git@github.com:transip/transip-api-php.git"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.