Edit DNS Zone Records By PHP Code

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,224
463
  • Like
Reactions: Saeb

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,224
463
You can upload the PHP class using FTP if you do not have root access, however it's likely a good idea to consult with your hosting provider to get their input. It's available at:

https://github.com/CpanelInc/xmlapi-php

Thank you.
 

Saeb

Member
Sep 18, 2015
12
0
1
Iran
cPanel Access Level
Website Owner
Code:
include "xmlapi.php";
$cpanel = new CPANEL(); // Connect to cPanel - only do this once.
// Edit a type "A" zone record on "example.com"
$edit_zone_record = $cpanel->api2(
  'ZoneRecord', 'edit_zone_record',
array(
  'Line' => '10',
  'domain' => 'domain.ir',
  'name' => 'sub',
  'type' => 'A',
  'address' => '10.10.10.10',
  'ttl' => '10',
  'class' => 'IN',
  )
);
what is line?
 
Last edited by a moderator:

Saeb

Member
Sep 18, 2015
12
0
1
Iran
cPanel Access Level
Website Owner
i so dissied
i include xmlapi.php
error in error_log wrote "fetal error"
__________________
my codes:
Code:
include "xmlapi.php";
$cpanel = new CPANEL(); // Connect to cPanel - only do this once.
// Add a type "A" zone record to "example.com"
$add_zone_record = $cpanel->api2(
  'ZoneRecord', 'add_zone_record',
array(
  'domain' => 'domain.ir',
  'name' => 'sub',
  'type' => 'A',
  'address' => '10.10.10.10',
  'ttl' => '10',
  'class' => 'IN',
  )
);
print_r($add_zone_record);
______________________
why dont work?
my host just use 2082 port
 
Last edited by a moderator:

Saeb

Member
Sep 18, 2015
12
0
1
Iran
cPanel Access Level
Website Owner
now errored:
[18-Sep-2015 20:25:44 Asia/Tehran] PHP Fatal error: Class 'CPANEL' not found in /home/khazrait/public_html/dns/tst.php on line 3
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,224
463
Could you let us know the exact steps you took to upload the PHP client class to your account?

Thank you.
 

Saeb

Member
Sep 18, 2015
12
0
1
Iran
cPanel Access Level
Website Owner
first uploaded xmlapi.php to dns folder at root
and create a file for write script to add/change/delete zone.
i write include xmlapi.php in that.
and wrote this codes:

Code:
<?php
include "xmlapi.php";
$cpanel = new CPANEL(); // Connect to cPanel - only do this once.
// Add a type "A" zone record to "example.com"
$add_zone_record = $cpanel->api2(
  'ZoneRecord', 'add_zone_record',
array(
  'domain' => 'example.ir',
  'name' => 'sub',
  'type' => 'A',
  'address' => '10.10.10.10',
  'ttl' => '10',
  'class' => 'IN',
  )
);
print_r($add_zone_record);
?>
code page url: - Removed -
Could you let us know the exact steps you took to upload the PHP client class to your account?

Thank you.
 
Last edited by a moderator:

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,224
463
  • Like
Reactions: Saeb

Saeb

Member
Sep 18, 2015
12
0
1
Iran
cPanel Access Level
Website Owner
thanks for reply and sorry for late answer

why show this problem?
Code:
[21-Apr-2016 17:49:12 Asia/Tehran] PHP Fatal error:  Uncaught exception 'Exception' with message 'No host defined' in /home/khazrait/public_html/aa/cp/xmlapi.php:237
Stack trace:
#0 /home/khazrait/public_html/aa/cp/Examples/listzones_example.php(30): xmlapi->__construct(false)
#1 {main}
  thrown in /home/khazrait/public_html/aa/cp/xmlapi.php on line 237
and i must fill this parameters at "xmlapi.php"?
PHP:
    private $debug            = false;

    // The host to connect to
    private $host                =    '127.0.0.1';

    // the port to connect to
    private $port                =    '2087';

    // should be the literal strings http or https
    private $protocol        =    'https';

    // output that should be given by the xml-api
    private $output        =    'simplexml';

    // literal strings hash or password
    private $auth_type     = null;

    //  the actual password or hash
    private $auth             = null;

    // username to authenticate as
    private $user                = null;

    // The HTTP Client to use

    private $http_client        = 'curl';
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,224
463
  • Like
Reactions: Saeb

Saeb

Member
Sep 18, 2015
12
0
1
Iran
cPanel Access Level
Website Owner
thank you.
now i need know how get a zone record line and how remove it. i can't find help and example for $xmlapi->api2_query
PHP:
print $xmlapi->api2_query(
  'ZoneEdit', 'remove_zone_record',
array(  'domain' => 'example.com',
  'line' => '42',
  )
);
I used to remove zone record but sho this error
HTML:
{"cpanelresult":{"apiversion":"2","error":"The “cpanel_jsonapi_func” parameter is required.","data":{"reason":"The “cpanel_jsonapi_func” parameter is required.","result":"0"},"type":"text"}}
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,224
463
Hello,

Could you let us know the full PHP code you are implementing, without the real authentication credentials?

Thank you.
 

Saeb

Member
Sep 18, 2015
12
0
1
Iran
cPanel Access Level
Website Owner
Hello
This is my Code, it is not more than .. ?!
but i set
PHP:
private $port         =   '2083';
because i cant access WHM. then i use cpanel port. add zone record worked.
PHP:
<?php
include 'xmlapi.php';
$xmlapi = new xmlapi('4.2.2.4');
$xmlapi->password_auth("userroot","passroot");
$xmlapi->set_output('json');
$xmlapi->set_debug(0);
$rand=rand(0,10000);
/*
print $xmlapi->api2_query("cPanelUserAccount", "ZoneEdit", "add_zone_record",  array(
  'domain' => 'mydomain.com', #Already an addon domain for the account
  'name' => 'test'.$rand, #Took this from a working SRV record I created using the advanced web editor
  'type' => 'CNAME',
  'cname' => 'cname.dom', #server1.mydomain.com has an existing A record
  'ttl' => '300',
  'class' => 'IN',)
  );
*/
//print $xmlapi->api2_query(  'ZoneEdit', 'fetchzones',);
print $xmlapi->api2_query(
    'ZoneEdit', 'remove_zone_record', '42',
  )
);


  ?>
 
Last edited:

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,224
463
thanks i found my resault until now. no need answer.
I'm happy to see you were able to resolve the problem. Feel free to share your findings here for others who may encounter a similar problem.

Thank you.