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.

Add subdomain using API

Discussion in 'cPanel Developers' started by LampedWeb, May 28, 2009.

  1. LampedWeb

    LampedWeb Member

    Joined:
    May 28, 2009
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    England
    cPanel Access Level:
    Root Administrator
    I've had a good search, but can't find an answer, sorry if I missed it.

    I need to ask CPanel to add a subdomain to an existing domain/account from PHP.

    In short, I want to type something like:

    $name = 'foobar';
    createsubdomain('blah.net',$name,'/public_html/');

    and have foobar.blah.net be created, and pointed to /public_html/

    This script will be run from blah.net, if that makes any difference.
     
  2. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    You may want to refer to this thread on the topic: http://forums.cpanel.net/f42/creating-sub-domains-external-scripts-77446.html
     
  3. LampedWeb

    LampedWeb Member

    Joined:
    May 28, 2009
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    England
    cPanel Access Level:
    Root Administrator
  4. LampedWeb

    LampedWeb Member

    Joined:
    May 28, 2009
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    England
    cPanel Access Level:
    Root Administrator
    PHP:
    require_once('xmlapi.php.inc');
    $xmlapi = new xmlapi('127.0.0.1');
    $xmlapi->password_auth('root','password');
    $xmlapi->set_debug(1);
    print 
    $xmlapi->api1_query('accountname','SubDomain','addsubdomain',array('sub','domain.com',0,0,'/public_html/folder'));
    Seems to work now, I hope this will be useful to someone in the future.
     
  5. mongoose643

    mongoose643 Registered

    Joined:
    Jun 4, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Thank you for posting that little snippet! I found it most useful.

    Where on Earth did you find the Subdomain module information... I only found these pages:

    XmlApi < AllDocumentation/AutomationIntegration < TWiki

    Api2DeleteSubdomain < AllDocumentation/AutomationIntegration < TWiki

    The second one doesn't even actually describe the Sudomain deletion process like the URL suggests - it actually details the StatsBar::stat function.

    I may also need to find information on all the other modules as time goes on but right now I'm in the dark about where to locate module names and their respective function lists.

    I especially need to get access to a list of subdomains for an account if at all possible.
     
  6. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Until the documentation for API1 and API2 are complete, you will find most of your code snippets for cPanel API functions here on the forums. If you want to look for these API calls yourself, keep in mind that the X3 theme uses these APIs itself so you can just look at the source to the X3 theme to find API calls. You can find the X3 theme's source on your server at /usr/local/cpanel/base/frontend/x3/.

    The code for listing subdomains is a API2 function:
    Code:
    <?cp SubDomain::listsubdomains() ?>
    You can pass an optional parameter "regex" to list subdomains matching a Perl regular expression.
     
  7. jhyland87

    jhyland87 Well-Known Member

    Joined:
    Dec 8, 2008
    Messages:
    153
    Likes Received:
    0
    Trophy Points:
    16
    How about adding parked domains?
     
  8. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    That is an API1 function:

    Code:
    <cpanel Park="park($FORM{'domain'})">
     
  9. jhyland87

    jhyland87 Well-Known Member

    Joined:
    Dec 8, 2008
    Messages:
    153
    Likes Received:
    0
    Trophy Points:
    16
    That works perfectly to add, delpark is to delete the parked domain?

    EDIT:

    unpark :)
     
    #9 jhyland87, Jun 5, 2009
    Last edited: Jun 5, 2009
  10. Luke Carrier

    Luke Carrier Active Member

    Joined:
    May 27, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    1
    As has been suggested above, look at the source of the relevant pages in cPanel's frontend ;)
     
  11. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Yes, you would use the unpark function.
     
  12. buzzybee

    buzzybee Registered

    Joined:
    Sep 3, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    x3 automatically adding subdomains with a php script

    I had problems with the x3 theme. When I ran an API call with a PHP script, I checked the default directory (named to the same name of the subdomain), the API kept adding a special character after the directory name. I could see the character in my SSH browser when I click on properties of the directory. The solution was to set the directory name + public_html/ in the string posted to the API like so....

    $request = "/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$domain&domain=blogdoc2&dir=public_html/blogdoc2&go=Create";

    $result = subd('localhost',2082,$cpaneluser,$cpanelpass,$request);

    $show = strip_tags($result);

    echo $result;
     
  13. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    It looks like you are using a technique called page scraping to achieve your goals rather than using cPanel's APIs. Also, our APIs are not theme-specific, meaning they will continue to operate regardless of what skins or themes you are using.

    I recommend you use our APIs instead as a means of avoiding these issues. To do this via our APIs, you would use the following API1 call:

    HTML:
    <cpanel SubDomain="addsubdomain(domain,rootdomain,0,0,dir)">
    Here's the relevant information if you want to call this function:

    API version: 1

    Module: SubDomain

    Function: addsubdomain

    Parameter 1: the subdomain to be created (i.e. subdomain in subdomain.example.com)

    Parameter 2: the domain within which the subdomain is to be created (i.e. example.com in subdomain.example.com)

    Parameter 3: 0

    Parameter 4: 0

    Parameter 5: The directory this subdomain should serve content from (relative to the user's home directory).

    We have documentation on how you can call this function remotely at XML and JSON APIs but you may find it easier to use a PHP 5 class my colleague Matt has assembled to make it easier to interface with our APIs. You can find his PHP class at: http://forums.cpanel.net/f42/xmlapi-php-class-111897.html
     
  14. omtechnologies

    omtechnologies Registered

    Joined:
    Aug 27, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Simple PHP Script for creating subdomain

    Hi,

    In case you have a cpanel based account, then you can easily create subdomain using php. Here is the link that shows how to create subdomain using PHP.

    Creating Sub Domains automatically using PHP | PHP Third Party APIs | PHP Tutor - Free PHP Tutorials

    This site has more quick php tutorials to various other whm and cpanel functionalies so you can check them out too...


    --
    Om Technologies
    The best web development php company
     
  15. yuenmar

    yuenmar Registered

    Joined:
    Jun 26, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    hi,
    where can i get the xmlapi.php.inc?

    thanks
     
  16. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi yuenmar,

    You can get xmlapi.php on the cPanel github page here.

    Regards,
    -DavidN
     
  17. jayapalchandran

    jayapalchandran Registered

    Joined:
    Oct 3, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
  18. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
  19. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    What he said.
    Plus, that code has it hardcoded to use port 2082, which is non-SSL. Meaning that anyone sniffing the traffic can base64 decode the header string and get your cpanel credentials.

    pretty sure this snippet is not the safest. Also, you're likely find that some hosting providers have disabled PHP's system() function (but that may not be an issue depending on your PHP and ini file). But that aside, the author provides NO sanitization of $cPanelUser or $usbDomain. And lastly, IIRC, subdomains can technically be served from other directories that public_html/subdomains/

    Code:
    $passToShell = "rm -rf /home/" . $cPanelUser . "/public_html/subdomains/" . $subDomain;
        system($passToShell);
    
    Use cPanel APIs do all the work...think about it: what do you thing is being done when you request dodeldomain.html or doadddomain.html? Those HTML pages provide forms (and form submission digestion) which calls the cPanel APIs. If you need to script cPanel action, there's a 98% chance that you can do it with APIs...and subdomains are not part of the remaining 2%.

    Regards,
    -DavidN
     
Loading...

Share This Page