The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Edit DNS Zone Records By PHP Code

Discussion in 'cPanel Developers' started by Saeb, Sep 18, 2015.

  1. Saeb

    Saeb Member

    Joined:
    Sep 18, 2015
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    How Can Edit/Add/Delete DNS Zone Record by PHP?
    Best Regards
     
    #1 Saeb, Sep 18, 2015
    Last edited by a moderator: May 7, 2016
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,774
    Likes Received:
    663
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Saeb likes this.
  3. Saeb

    Saeb Member

    Joined:
    Sep 18, 2015
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    thanks
    but how can define(Or Include) CPANEL class?
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,774
    Likes Received:
    663
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    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.
     
  5. Saeb

    Saeb Member

    Joined:
    Sep 18, 2015
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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?
     
    #5 Saeb, Sep 18, 2015
    Last edited by a moderator: Sep 25, 2015
  6. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,774
    Likes Received:
    663
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    It's documented in the initial URL you were provided:

    The line in the zone file to edit.

    Thank you.
     
  7. Saeb

    Saeb Member

    Joined:
    Sep 18, 2015
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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
     
    #7 Saeb, Sep 18, 2015
    Last edited by a moderator: Sep 25, 2015
  8. Saeb

    Saeb Member

    Joined:
    Sep 18, 2015
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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
     
  9. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,774
    Likes Received:
    663
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  10. Saeb

    Saeb Member

    Joined:
    Sep 18, 2015
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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 -
     
    #10 Saeb, Sep 25, 2015
    Last edited by a moderator: Sep 25, 2015
  11. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,774
    Likes Received:
    663
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Saeb likes this.
  12. Saeb

    Saeb Member

    Joined:
    Sep 18, 2015
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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';
     
  13. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,774
    Likes Received:
    663
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Saeb likes this.
  14. Saeb

    Saeb Member

    Joined:
    Sep 18, 2015
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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"}}
     
  15. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,774
    Likes Received:
    663
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  16. Saeb

    Saeb Member

    Joined:
    Sep 18, 2015
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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',
      )
    );


      
    ?>
     
    #16 Saeb, May 7, 2016
    Last edited: May 7, 2016
  17. Saeb

    Saeb Member

    Joined:
    Sep 18, 2015
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    thanks i found my resault until now. no need answer.
     
  18. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,774
    Likes Received:
    663
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    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.
     
Loading...

Share This Page