繁体   English   中英

PHP cPanel创建子域

[英]PHP cPanel Create Subdomain

因此,我尝试使用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=$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;
}

就像我说的那样,它给我一个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=$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;
}

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) . "\n\r";
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";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM