PHP Script to Edit DNS Records in CPanel

Alfo picture Alfo · May 27, 2012 · Viewed 12.5k times · Source

I would like to become self-sufficient, and therefore do without services such as DNSDynamic and DYNDNS. And I don't like paying for services that I could do myself.

So here's the scenario - I have my main website hosted with a hosting company. I also have a home server with my music, etc. on it. But the problem is that my ISP (BT) does not offer consumers Static IP addresses.

I would like to have a subdomain of my main domain (which points to my main host) point to my home IP. This is done with a simple A record - which I have done myself.

This boils down to me wanting to make a PHP script (run by a cron job on my home server) to update the A records in cPanel to the current IP of my home server.

Here's some sample code - of course the bit that's missing is the API Code to communicate with cPanel, and I'd be very thankful to anyone who could fill in the gaps for me.

<?php
    $current_ip = file_get_contents("http://mydomain.com/getip.php");
    $username = "CPANEL_USERNAME";
    $password = "CPANEL_PASSWORD";
    $domain = "CPANEL_DOMAIN";

    $request = file_get_contents("http://someapipage?username=".$username."&pw=".$password."&domain=".$domain."&ip=".$current_ip);

?>

The code in http://mydomain.com/getip.php is something along the lines of simply

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

I already have the grasp of how to set up a cron job, as my home server is running Ubuntu, and I have read tutorials that call my cron.php in my localhost directory using wget.

I have tried this link but I couldn't fathom what he was doing. Thanks in advance.

Answer

Scott Stevens picture Scott Stevens · Dec 21, 2012

I have just written this library for cPanel's JSON-API based on their documentation and the jordih.net links to that documentation. It's not so well documented, but the gist of it is:

Create a zone_records object by calling

$zones = new zone_records("cpaneluser", "pass", "website_to_login", "domain_of_records")

Note that the website to login is usually 127.0.0.1 if you are running this from the server you want to change the records on.

Once called, you can then access a member $zones->DNSrecords. This contains an array of the DNS A records and CNAME records (both of which are DNSrecord objects). The others (except TXT) are irrelevant since you cannot edit them without extra additions (functions) to the classes.

Each DNSrecord has some members (e.g target, ttl, name, type) which, while private, can be accessed via $record->ttl because I have added the "magic" __get method. The "magic" __set method is implemented to change the ttl and target only (you cannot change other properties using this API function, and the object will raise an exception if you try to do so).

You can use

$zones->addrecord($type, $target, $name, $ttl)

to add a record, or

$zones->deleterecord($line)

to delete the record that is on line $line in the zone file - you can find it via $record->line.

If you want to make some of your own queries in the ZoneEdit module, you can call

$zones->doquery("function_from_API", array("parameters=>"here"), array("headers"=>"here"))

and it will return the cPanel response (as will the addrecord and deleterecord methods). Finally, I would advise you to use try {...} catch (Exception $e) {...} since my objects throw exceptions if/when something goes wrong (you can edit them out of course).

This code is in the public domain - you can get it at https://github.com/ShadowLNC/cpanel_dns (classdns.php is the main file, and dns_update.php shows an example).