PHP CPANEL创建子域



因此,我正在尝试使用PHP自动为我的网站创建子域。我尝试了以下代码,但它给了我301错误,并将我重定向到我的CPANEL登录

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;
}

就像我说的那样,它给了我301错误,并将其重定向到example.com:2083,而不仅仅是在代码中进行操作,而不是让我手动登录CPANEL。任何帮助将不胜感激!

答案:摆弄我的代码后,我意识到端口2082和端口2083是相同的,除了2082没有https://,所以我将端口更改为2082,并且可以使用!

代码:

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;
}

api 1现在已死。同样,通过非安全连接(即2082而不是2083(传递CPANEL密码也是一个非常糟糕的主意。接下来,您知道有人会劫持您的Cpanel帐户!

但是,结合此处提供的代码以进行身份验证以及在这里添加子域,为我们提供了以下脚本,似乎可以正常工作:

<?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;
?>

结果应该是这样的:

{"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}}}

要删除子域,请通过上述脚本运行此查询:

$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

要删除目录,请运行以下内容:

 $deletedir =  "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_module=Fileman&cpanel_jsonapi_func=fileop&op=unlink&sourcefiles=$directory";

noel的答案从2018年起的可能性更大,但是如果您来这里寻找如何使用cpanel api添加子域,请在此链接上开始以查看为子域模块addsubdomain接受CPANEL API VER 2接受的参数。

以下是一个对我来说很好效果的示例。

$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);

请注意,如果您想查看CPANEL作为$result的响应,请在curl_close($curl);

之后放置print $result;

最新更新