Actually, Forum User Boldamunk has worked out the hard part, with this example code that uses PHP to add an email forwarding address directly using curl to manipulate the Control Panel.
I threw a couple of trivial modifications at that code, and have successfully added domains/subdomains via PHP using the same method. This is only a "proof of concept", but it works (under PHP5) and should give you what you need to code an "automated process" if you wish to do so (absolutely *no* security considerations were covered with this code, etc.).
Note that this does *not* use an established API, and could be "borken" by a change in the DH Control Panel operation. Additionally, to use the code you will need to "view source" on the "Add a Domain" page of the Control Panel, or use another tool, to determine the numeric usid code assigned to your named user(s) as listed in the "select box" presented in the panel for the FTP/Run CGI as user.
It also sets the new domain up with the "defalut" DH settings for PHP version, www usagage, fastcgi usage, and mod_security usage, and the "standard" DH home/user/sub.yourdomain.tld path (which is different than the home/user/yourdomain.tld/sub.yourdomain.tld/ path that you seem to want) - you will have to tweak as necessary if you want other options
Any way, here is some working code that will create a domain/subdomain via PHP:
<? // Set your DH Control Panel user and password here $USER = "yourWebID"; $PASS = "yourpassword"; // Set the usid of the FTP user/run CGI as User here - view the form source from the panel to obtain the //numberic usid numbers of your named users $usid = "123456"; // Note: I am using this script to create an new Domain/subdomain via the panel $new_subdomain = "whatever.yourdomain.tld"; # new Domain/subdomain
function show_curl_errors ($chan) { # # Displays cURL error messages when needed. # if (curl_errno($chan) != 0) { print "Last cURL error : ".curl_errno($chan)." \n"; print "Last cURL error : ".curl_error($chan)." \n"; } }
# I'm using two cookie jars, one for before the login, one for after. Not sure if this is neccessary however $cookie_jar1 = tempnam($_SERVER['DOCUMENT_ROOT']."/admin",'cookie1'); $cookie_jar2 = tempnam($_SERVER['DOCUMENT_ROOT']."/admin",'cookie2');
# standard browser header $header_array['Host'] = "panel.dreamhost.com"; $header_array['Accept'] = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; $header_array['Accept-Language'] = "en-us,en;q=0.5"; $header_array['Accept-Encoding'] = "gzip,deflate"; $header_array['Accept-Charset'] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7"; $header_array['Keep-Alive'] = "300"; $header_array['Connection'] = "keep-alive"; $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7";
# start session on index/login page $url = "https://panel.dreamhost.com/"; $ref_url = "";
$c = curl_init($url); curl_setopt($c, CURLOPT_VERBOSE, 1); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_HEADER, 1); curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($c, CURLOPT_HTTPHEADER, $header_array); curl_setopt($c, CURLOPT_USERAGENT,$user_agent); curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar1); $page = curl_exec($c); show_curl_errors ($c); curl_close($c);
# Submit login request $ref_url = $url; $url = "https://panel.dreamhost.com/index.cgi"; $param_string = "Nscmd=Nlogin&username=$USER&password=$PASS";
$c = curl_init($url); curl_setopt($c, CURLOPT_VERBOSE, 1); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_HEADER, 1); curl_setopt($c, CURLOPT_REFERER, $ref_url); curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($c, CURLOPT_HTTPHEADER, $header_array); curl_setopt($c, CURLOPT_USERAGENT,$user_agent);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar1); curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar2); curl_setopt($c, CURLOPT_POST, 1); curl_setopt($c, CURLOPT_POSTFIELDS, $param_string); $page = curl_exec($c); #show_curl_errors ($c); curl_close($c); #print "Login Page\n"; unlink($cookie_jar1);
# Resubmit the index page request - often get a blank response from the login page. This makes sure the session is legit $ref_url = $url; $url = "https://panel.dreamhost.com/index.cgi";
$c = curl_init($url); curl_setopt($c, CURLOPT_VERBOSE, 1); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_HEADER, 1); curl_setopt($c, CURLOPT_REFERER, $ref_url); curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($c, CURLOPT_HTTPHEADER, $header_array); curl_setopt($c, CURLOPT_USERAGENT,$user_agent); curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar2); curl_setopt($c, CURLOPT_POST, 0); $page = curl_exec($c); #show_curl_errors ($c); curl_close($c); #print "ReGet Login Page\n";
# ask for the "Add a New Domain/Subdomain" page. #$ref_url = $url; $url = "https://panel.dreamhost.com/index.cgi?tree=domain.manage¤t_step=Index&next_step=ShowAddhttp&domain=";
$c = curl_init($url); curl_setopt($c, CURLOPT_VERBOSE, 1); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_HEADER, 1); curl_setopt($c, CURLOPT_REFERER, $ref_url); curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($c, CURLOPT_HTTPHEADER, $header_array); curl_setopt($c, CURLOPT_USERAGENT,$user_agent); curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar2); curl_setopt($c, CURLOPT_POST, 0); $page = curl_exec($c); #show_curl_errors ($c); curl_close($c); #print "Add Domain/SubDomain Page\n";
# submit the domain/subdomain page using the variables we have previously set up. # Note: I'm only setting up addresses that are forwarded to an existing address. $ref_url = $url; $url = "https://panel.dreamhost.com/index.cgi?"; $param_string = "tree=domain.manage¤t_step=ShowAddhttp&next_step=AddHttp&tree=domain.manage&domain_type=cgi&domain=$new_subdomain&php_version=5&mod_security_on=1&usid=$usid&relpath=$new_subdomain&www_redir=0";
$c = curl_init($url); curl_setopt($c, CURLOPT_VERBOSE, 1); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_HEADER, 1); curl_setopt($c, CURLOPT_REFERER, $ref_url); curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($c, CURLOPT_HTTPHEADER, $header_array); curl_setopt($c, CURLOPT_USERAGENT,$user_agent); curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar2); curl_setopt($c, CURLOPT_POST, 1); curl_setopt($c, CURLOPT_POSTFIELDS, $param_string); $page = curl_exec($c); #show_curl_errors ($c); curl_close($c);
print "If the Domain/Subdomain was set up correctly, $new_subdomain will appear in Control Panel";
unlink($cookie_jar2);
exit; ?>
--rlparker
|