Skip to main content

Create a subdomain on account creation

Comments

5 comments

  • cPanelMichael
    Hello :) You may find the following documents helpful: Software Development Kit Home PHP API Class Could you clarify which parts you find confusing? Thank you.
    0
  • DanH42
    The hook API seems straightforward enough; I think all I need to do is run "sudo /usr/local/cpanel/bin/manage_hooks add script /scripts/myscript --category Whostmgr --event Accounts::Create" to get my script called and passed details when an account is created. But dealing with the API has been another story. There's references to UAPI and LiveAPI and cpphp and phpcp and API 1 vs 2 requests, and it's around there I start getting lost. The examples in the CpanelInc/xmlapi-php repo don't do exactly what I need (not that I'd expect them to), and while I think SubDomain::addsubdomain is what I'm after, there's an API 1 and an API 2 version, and I'm unclear on which one I want, or exactly how to call it. I think I'm close, but I'm having trouble getting all the pieces put together.
    0
  • DanH42
    Update: after a fair amount of trial-and-error, I got a working solution. First, the command in the post above that I thought would add the proper hook gave me an error. I managed to get it to run without errors and print a status message, but my script still wasn't triggered on account creation. After looking at the logs generated while creating an account, I noticed cPanel always calls a script at /usr/local/cpanel/scripts/postwwwacct, but no file exists there. I symlinked my script to that location, and it got called properly after an account was created. There's also a corresponding "prewwwacct" script that's called just before creating the new account that some might find useful. The documentation wasn't specific, but I assumed account information would be passed in as a JSON or XML string on stdin. That was not the case. Instead, it looks like the information is passed via command-line arguments something like this: # /usr/local/cpanel/scripts/postwwwacct locale en dkim 1 max_defer_fail_percentage unlimited (...)
    That's sort of strange, but I worked out the following PHP code to give me an associative array: [PHP]$args = array(); for($i = 1; $i < count($_SERVER['argv">); $i += 2){ if(isset($_SERVER['argv">[$i + 1])) $args[$_SERVER['argv">[$i]] = $_SERVER['argv">[$i + 1]; }[/PHP] This gives you a variable called $args that lets you look up properties like username, domain, password, etc. What I needed to do was create a subdomain called "staging." with its content directory in ~user/staging_html for every new account. Ultimately, this was the code I worked out (using the xmlapi.php Michael linked to): [PHP]#!/usr/local/cpanel/3rdparty/bin/php ); $i += 2){ if(isset($_SERVER['argv">[$i + 1])) $args[$_SERVER['argv">[$i]] = $_SERVER['argv">[$i + 1]; } if(isset($args['user">) && isset($args['domain">)){ $xmlapi = new xmlapi("127.0.0.1"); $xmlapi->hash_auth("root", "REMOTE ACCESS KEY FOR ROOT"); $params = array("staging", $args['domain">, 0, 0, "staging_html"); $xmlapi->api1_query($args['user">, "SubDomain", "addsubdomain", $params); } ?>[/PHP]
    0
  • grayloon
    I want to do something similar. I need to add about 60 subdomains to a single account. I want to do this on-demand. Could someone guide me to the correct API to use for this?
    0
  • cPanelMichael
    [quote="grayloon, post: 1768962">I want to do something similar. I need to add about 60 subdomains to a single account. I want to do this on-demand. Could someone guide me to the correct API to use for this?
    Are the previous posts to this thread helpful? Thank you.
    0

Please sign in to leave a comment.