With simple, one-line method calls, You can use this class in your scripts to do the following:
One use of this class is to automatically add new domains to the DNS when customers signup for your webhosting. In addition to being an excellent server-side web scripting language, PHP is also a powerful shell scripting language. So you can use this class in a web application or a shell script.
This class uses my popular class_http--a powerful and easy to use "screen-scraping" class. You can learn more about class_http by checking out the class_http documentation.
Let's cut to the chase. Code examples are what you need. Here is an example to add a new domain to the Server Beach DNS with Server Beach as the DNS master.
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Add new domain with Server Beach as both master and slave.
if (!$sbdns->add_domain("mynewdomain.com", "192.168.0.12", true)) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
Yes, that really is it. The class does all the heavy lifting. To add a domain with SB serving only as slave DNS, the only difference is the third parameter of the add_domain() method is set to false.
Note that when setting SB as master (third parameter = true), the second parameter is the IP address of the server where the domain is hosted. When setting SB as slave (third parameter = false), the second parameter is the IP address of your master DNS server.
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Add new domain with Server Beach as slave only.
if (!$sbdns->add_domain("mynewdomain.com", "192.168.0.12", false)) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
All the other methods of the class require you to first set a domain to work with. This is the same as clicking the "GO" link next to a domain in the SB DNS Tool.
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("oneofmyexistingdomains.com")) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
Now that you know how to set the domain, let's see how to add zone records. There are three different ways to create your A and CNAME records. The four examples below show the three variations. Choose the right one for your specific task.
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
die($sbdns->status_msg);
}
// 1. Add a CNAME record for the domain.
// test1 will be a CNAME for anexistingdomain.com.
if (!$sbdns->add_record("test1")) {
die($sbdns->status_msg);
}
// 2. Add an A record to the zone.
// An A record will be created because you are passing an IP address
// as the 2nd parameter.
// test2 will be an A record for the anexistingdomain.com zone.
if (!$sbdns->add_record("test2", "192.168.1.47")) {
die($sbdns->status_msg);
}
// 3. Add a CNAME record to a subdomain of the domain.
// test3 will be a CNAME for test2.anexistingdomain.com.
if (!$sbdns->add_record("test3", "test2.anexistingdomain.com")) {
die($sbdns->status_msg);
}
// 4. Add a CNAME record to an external subdomain or domain.
// This is really the same thing as #3 above, but since the SB DNS Tool
// distinguishes between these two situations, I show an example of each.
// test4 will be a CNAME for www.google.com.
if (!$sbdns->add_record("test4", "www.google.com")) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
It is not necessary to add an MX record for your root domain because if an MX record does not exist, the A record will be used. So if your domain name is mydomain.com, you do not need to create an MX record for mydomain.com unless you host your mail on a server other than the web server. In which case, you would pass "mydomain.com" as the first parameter of the add_mail_exchanger() method below. If the owner is a subdomain instead of the root domain, then you would simply pass the subdomain name as the first parameter. For example, if bob's email address is "bob@home.mydomain.com", you should pass "home" as the first parameter (owner).
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
die($sbdns->status_msg);
}
// Set a Mail Exchanger (MX record) for a domain.
// First parameter is the mail domain (the part after @ in the email address).
// This can be any A record in the current domain zone. The class enforces this.
// Second parameter is the name of the mail server. (NOT an IP address)
// Third parameter is the preference number. Not required, defaults to 10.
if (!$sbdns->add_mail_exchanger("anexistingdomain.com", "mail.someserver.com", 10)) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
Delete a domain zone completely. Of course make sure you really don't want the domain zone anymore!
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
die($sbdns->status_msg);
}
// Delete the currently selected domain.
if (!$sbdns->delete_domain()) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
die($sbdns->status_msg);
}
// First parameter is the subdomain name. Do not append main domain name.
// Second parameter is the target. Same rules apply as the add_record() method.
// Note that you cannot change an A record to a CNAME, but you can change a
// CNAME to an A.
if (!$sbdns->update_record("test1", "192.168.1.45")) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
die($sbdns->status_msg);
}
// Modify an existing Mail Exchanger (MX) record.
// First parameter is the mail domain (the part after @ in the email address).
// This can be any A record in the current domain zone. The class enforces this.
// Second parameter is the name of the mail server. (NOT an IP address)
// Third parameter is the preference number. Not required, defaults to 10.
if (!$sbdns->update_mail_exchanger("anexistingdomain.com", "mail2.anexistingdomain.com", 10)) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
die($sbdns->status_msg);
}
// Delete an existing A or CNAME record.
if (!$sbdns->delete_record("test1")) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
die($sbdns->status_msg);
}
// Delete an existing Mail Exchanger (MX) record.
if (!$sbdns->delete_mail_exchanger("anexistingdomain.com")) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
die($sbdns->status_msg);
}
// Change the hostmaster email address.
if (!$sbdns->change_hostmaster_email("webmaster@mydomain.com")) {
die($sbdns->status_msg);
}
echo $sbdns->status_msg;
?>
There are 2 other methods that you may find useful.
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
die($sbdns->status_msg);
}
// Display the zone file text.
if (!$zone_text = $sbdns->get_zone_file()) {
die($sbdns->status_msg);
}
echo "<pre>".$zone_text."</pre>";
?>
The get_zone_records() method creates an array of all the zone's records. To see the structure of this array, just print out the array as shown in the example below.
<?php
// Include the class.
require_once("class_sbdns.php");
// Instantiate the sbdns object.
$sbdns = new sbdns();
// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }
// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
die($sbdns->status_msg);
}
// Retrieve an array containing all the zone's records.
if (!$sbdns->get_zone_records()) {
die($sbdns->status_msg);
}
echo "<pre>";
print_r($sbdns->records);
echo "</pre>";
?>
If you want to know more about any of the methods and their parameters, simply read the source code. It is heavily commented.
The hope is that Server Beach will make this class obsolete by providing the customers with a real API to the DNS. But until then, this is a sweet work-around.