skip to Main Content

So I am trying to automatically create subdomains for my website using PHP. I tried the following code but it gives me a 301 error and redirects me to my cPanel login

function createDomain($domain) {
    // your cPanel username
    $cpanel_user = 'User';

    // your cPanel password
    $cpanel_pass = 'Pass';

    // your cPanel skin
    $cpanel_skin = 'paper_lantern';

    // your cPanel domain
    $cpanel_host = 'example.com';

    // subdomain name
    $subdomain = $domain;

    // directory - defaults to public_html/subdomain_name
    $dir = 'public_html/user_site';

    // create the subdomain

    $sock = fsockopen($cpanel_host,2083);
    if(!$sock) {
        print('Socket error');
        exit();
    }

    $pass = base64_encode("$cpanel_user:$cpanel_pass");
    $in = "GET /frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$cpanel_host&domain=$subdomain&dir=$dirrn";
    $in .= "HTTP/1.0rn";
    $in .= "Host:$cpanel_hostrn";
    $in .= "Authorization: Basic $passrn";
    $in .= "rn";

    fputs($sock, $in);
        while (!feof($sock)) {
        $result .= fgets ($sock,128);
    }
    fclose($sock);

    return $result;
}

Like I said it gives me a 301 error and redirects to example.com:2083 instead of just doing it in the code and not having me login to the cPanel manually. Any help would be greatly appreciated!

3

Answers


  1. Chosen as BEST ANSWER

    ANSWER: After fiddling with my code I realized that port 2082 and port 2083 are the same except that 2082 has no https:// so I changed the port to 2082 and it worked!

    CODE:

    function createDomain($domain) {
        // your cPanel username
        $cpanel_user = 'User';
    
        // your cPanel password
        $cpanel_pass = 'Pass';
    
        // your cPanel skin
        $cpanel_skin = 'paper_lantern';
    
        // your cPanel domain
        $cpanel_host = 'example.com';
    
        // subdomain name
        $subdomain = $domain;
    
        // directory - defaults to public_html/subdomain_name
        $dir = 'public_html/user_site';
    
        // create the subdomain
    
        $sock = fsockopen($cpanel_host,2082);
        if(!$sock) {
            print('Socket error');
            exit();
        }
    
        $pass = base64_encode("$cpanel_user:$cpanel_pass");
        $in = "GET /frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$cpanel_host&domain=$subdomain&dir=$dirrn";
        $in .= "HTTP/1.0rn";
        $in .= "Host:$cpanel_hostrn";
        $in .= "Authorization: Basic $passrn";
        $in .= "rn";
    
        fputs($sock, $in);
            while (!feof($sock)) {
            $result .= fgets ($sock,128);
        }
        fclose($sock);
    
        return $result;
    }
    

  2. API 1 is now dead. Also passing your cpanel password through a non-secure connection (ie port 2082 instead of 2083) is a very bad idea. Next thing you know someone will have hijacked your cpanel account!

    However, combining the codes given here for authentication and here for adding a subdomain, gives us the following script which seems to work just fine:

    <?php 
    
    $cpanelsername = "example";
    $cpanelpassword = "**********";
    $subdomain = 'newsubdomain';
    $domain = 'example.com';
    $directory = "/public_html/$subdomain";  // A valid directory path, relative to the user's home directory. Or you can use "/$subdomain" depending on how you want to structure your directory tree for all the subdomains.
    
    $query = "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_func=addsubdomain&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_version=2&domain=$subdomain&rootdomain=$domain&dir=$directory";   
    
    $curl = curl_init();                                // Create Curl Object
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);       // Allow self-signed certs
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);       // Allow certs that do not match the hostname
    curl_setopt($curl, CURLOPT_HEADER,0);               // Do not include header in output
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);       // Return contents of transfer on curl_exec
    $header[0] = "Authorization: Basic " . base64_encode($whmusername.":".$whmpassword) . "nr";
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);    // set the username and password
    curl_setopt($curl, CURLOPT_URL, $deletedir);            // execute the query
    $result = curl_exec($curl);
    if ($result == false) {
        error_log("curl_exec threw error "" . curl_error($curl) . "" for $query");   
                                                        // log error if curl exec fails
    }
    curl_close($curl);
    
    print $result;
    
    ?>
    

    The result should be something like this:

    {"cpanelresult":{"func":"addsubdomain","event":{"result":1},"apiversion":2,"module":"SubDomain","data":[{"reason":"The subdomain “newsubdomain.example.com” has been added.","result":1}],"preevent":{"result":1},"postevent":{"result":1}}}
    

    To delete the subdomain, run this query through the above script:

    $deletesub =  "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_func=delsubdomain&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_version=2&domain=".$subdomain.'.'.$domain."&dir=$directory";  //Note: To delete the subdomain of an addon domain, separate the subdomain with an underscore (_) instead of a dot (.). For example, use the following format: subdomain_addondomain.tld
    

    And to delete the directory, run this:

     $deletedir =  "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_module=Fileman&cpanel_jsonapi_func=fileop&op=unlink&sourcefiles=$directory";
    
    Login or Signup to reply.
  3. Noel’s answer from 2018 more than likely won’t work anymore, but if you’ve come here looking for how to use the Cpanel API to add a subdomain, start at this link to see the arguments accepted for the Cpanel API Ver 2 for the SubDomain module addsubdomain.

    Below is an example that worked perfectly well for me.

    $whmusername = "cpanel_username";
    $whmpassword = "cpanel_password";
    $subdomain = 'newsubdomain';
    $cpanel_ip = 'IP_ADDRESS'; //ip of cpanel or your_domain.com
    $domain = "your_domain.com";
    
    $query = "https://".$cpanel_ip."2083/json-api/cpanel?cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&cpanel_jsonapi_apiversion=2&dir=/public_html/".$subdomain.".".$domain."/&rootdomain=".$domain."&domain=".$subdomain."";
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl, CURLOPT_HEADER,0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
    $header[0] = "Authorization: Basic " . base64_encode($whmusername.":".$whmpassword) . "nr";
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_URL, $query);
    $result = curl_exec($curl);
    curl_close($curl);
    

    Note that if you want to see what Cpanel is returning as a response for $result, then place print $result; after curl_close($curl);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search