Skip to main content

Edit DNS Zone Records By PHP Code

Comments

17 comments

  • cPanelMichael
    Hello :) Is there a specific reason you need to edit a DNS zone through PHP instead of through cPanel? You could develop a custom PHP script that utilizes API to manipulate a zone. EX:
    0
  • Saeb
    thanks but how can define(Or Include) CPANEL class?
    0
  • cPanelMichael
    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:
    0
  • Saeb

    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?
    0
  • cPanelMichael
    what is line?

    It's documented in the initial URL you were provided: The line in the zone file to edit. Thank you.
    0
  • Saeb
    i so dissied i include xmlapi.php error in error_log wrote "fetal error" __________________ my codes:
    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
    0
  • Saeb
    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
    0
  • cPanelMichael
    Could you let us know the exact steps you took to upload the PHP client class to your account? Thank you.
    0
  • Saeb
    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:
    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.

    0
  • cPanelMichael
    You may want to start by using one of the example scripts at: xmlapi-php/api2_example_withargs.php at master " CpanelInc/xmlapi-php " GitHub This post may help with the authentication: Remote Authentication - cPanel (No WHM) - ListPopAccounts Try getting the example script working for a specific email account on your system so you are successful with an example script before proceeding to writing your own. Thank you.
    0
  • Saeb
    thanks for reply and sorry for late answer why show this problem?
    [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';[/PHP]
    0
  • cPanelMichael
    You should not have to manually modify the xmlapi.php file. It should match what's available for download at: xmlapi-php/xmlapi.php at master " CpanelInc/xmlapi-php " GitHub You may also find a thread like this helpful an example of the concept: xmlapi add SRV record Thank you.
    0
  • Saeb
    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', ) ); [/PHP] 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"}}[/HTML]
    0
  • cPanelMichael
    Hello, Could you let us know the full PHP code you are implementing, without the real authentication credentials? Thank you.
    0
  • Saeb
    Hello This is my Code, it is not more than .. ?! but i set [PHP]private $port = '2083';[/PHP] because i cant access WHM. then i use cpanel port. add zone record worked. [PHP]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', ) ); ?>[/PHP]
    0
  • Saeb
    thanks i found my resault until now. no need answer.
    0
  • cPanelMichael
    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.
    0

Please sign in to leave a comment.