PHP Script to Edit DNS Records in CPanel

Trainee Asked on October 12, 2019 in Cpanel.
Add Comment
1 Answer(s)

I have just written this library for cPanel’s JSON-API based on their documentation and the 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 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


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).

Reviewer Answered on October 12, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.