Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create subdomains using cURL?

I want to create sub-domains at my site. My hosting plan does not support subdomain wildcard entries. So, I thought to create it by using cURL in PHP. My problem is explained below:

This link is used to create subdomains:

https://cpanel.myhost.com/cpanel/indexcp.php?option=subdomains

This form at above link creates a subdomain:

<form method="post" action="/cpanel/indexcp.php?option=subdomains_add" name="domain">
        <table>
            <tr>
                <td align="right"><b>Subdomain</b> :&nbsp; </td>
                <td><input type="text" onchange="updatedir(this);" id="domain" name="DomainName" />&nbsp;.&nbsp;
                    <select name="domain_selector" style="width: 300px">
                        <option>mydomain.com</option>
                    </select>
                </td>
                <td><div id="domain_error" style="height: 16px; width: 16px"></div></td>
            </tr>
                    </table>
                </td>
                <td></td>
            </tr>
            <tr><td colspan="3"><br /></td></tr>
            <tr>
                <td>&nbsp;</td>
                <td><input class="input-button" id="subdomain_submit" type="submit" value="Create" name="B1" />

                </td>
                <td></td>
            </tr>
        </table>
    </form>

So, How do I create a subdomain from my cPanel using cURL??

like image 432
Tushar Rastogi Avatar asked Mar 16 '26 15:03

Tushar Rastogi


1 Answers

This code makes use of cPanel's JSON version of "API2" and can do what you need. I haven't tested it with a standard cPanel account, only with an account that has WHM access. I hope this helps.

Just change $subdomain and $rootdomain to what you require. Obviously update the user credentials too.

<?
$whmusername = "root";
$whmpassword = "12345luggage";
$subdomain = "mysubdomain";
$rootdomain = "example.com";

$query = "https://127.0.0.1:2087/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=".$subdomain."&rootdomain=".$rootdomain;

$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) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);    // set the username and password
curl_setopt($curl, CURLOPT_URL, $query);            // 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;

?>
like image 157
Peter B Avatar answered Mar 19 '26 04:03

Peter B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!