I'm trying to create a subdomain dynamically with php so that everytime someone creates a user they also create a subdomain.
I seems all the answers I can find is the same and they just don't work.
I.e this is the code most suggest:
function createDomain($domain) {
// your cPanel username
$cpanel_user = 'username';
$cpanel_pass = 'pass';
$cpanel_skin = 'paper_lantern';
$cpanel_host = 'mydomainname.com';
$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=$dir\r\n";
$in .= "HTTP/1.0\r\n";
$in .= "Host:$cpanel_host\r\n";
$in .= "Authorization: Basic $pass\r\n";
$in .= "\r\n";
fputs($sock, $in);
while (!feof($sock)) {
$result = fgets($sock, 128);
}
fclose($sock);
return $result;
}
createDomain('testing');
When I try to use the link directly in browser to see what happens, the cpanel tells me there was something wrong with the security token and I get a login form.
Therefore I tried making a call to generate a security token:
function createSession() { // Example details
$ip = "example.com";
$cp_user = "username";
$cp_pwd = "password";
$url = "https://example.com:2083/login";
$cookies = "/path/to/storage/for/cookies.txt";
// Create new curl handle
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Save cookies to
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$cp_user&pass=$cp_pwd");
curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute the curl handle and fetch info then close streams.
$f = curl_exec($ch);
$h = curl_getinfo($ch);
curl_close($ch);
// If we had no issues then try to fetch the cpsess
if ($f == true and strpos($h['url'],"cpsess")){
// Get the cpsess part of the url
$pattern="/.*?(\/cpsess.*?)\/.*?/is";
$preg_res=preg_match($pattern,$h['url'],$cpsess);
}
// If we have a session then return it otherwise return empty string
$token = (isset($cpsess[1])) ? $cpsess[1] : "";
}
$test = createSession();
It successfully creates the token. I then tried sending the security token through to the other call, so it would be something like this:
$token . "/frontend/paper_lantern/subdomain/doadddomain.html?rootdomain=" . $main_domain . "&domain=" . $sub_domain_name . "&dir=public_html/subdomains/" . $sub_domain_name
but nothing happens, no errors, no subdomain is created. How can I make this work?
After alot of trying and looking, I finally figured it out. I made it into a class so it would be easy to use for me, and anyone else looking.
Notes: - I put the private variables in the .env file. - The delete will delete the subdomain as well as the directory. - The delete does not delete the DNS that is created, still haven't looked into that yet.