How do I create subdomains using cURL?
I want to create subdomains on my site. My hosting plan does not support wildcard subdomain entries. So, I decided to create it 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 from the link above creates a subdomain:
<form method="post" action="/cpanel/indexcp.php?option=subdomains_add" name="domain">
<table>
<tr>
<td align="right"><b>Subdomain</b> : </td>
<td><input type="text" onchange="updatedir(this);" id="domain" name="DomainName" /> .
<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> </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 ??
+3
source to share
1 answer
This code uses the "API2" version of cPanel JSON and can do what you want. I have not tested it with a standard cPanel account, only an account with WHM access. Hope this helps.
Just change and to whatever you need. Obviously update the user's credentials. $subdomain
$rootdomain
<?
$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;
?>
+1
source to share