PHP cPanel 创建子域

问题描述 投票:0回答:4

所以我尝试使用 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。任何帮助将不胜感激!

php subdomain cpanel
4个回答
5
投票

答案: 摆弄我的代码后,我意识到端口

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

4
投票

API 1 现已失效。另外,通过非安全连接(即端口 2082 而不是 2083)传递 cpanel 密码也是一个非常糟糕的主意。接下来你知道有人会劫持你的 cpanel 帐户!

但是,结合here用于身份验证的代码和here用于添加子域的代码,为我们提供了以下似乎运行良好的脚本:

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

1
投票

Noel 在 2018 年的回答很可能不再有效,但如果您来这里寻找如何使用 Cpanel API 添加子域,请从 此链接 开始查看 Cpanel API 版本接受的参数2 个子域模块

addsubdomain

下面是一个对我来说非常有效的示例。

$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) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_URL, $query);
$result = curl_exec($curl);
curl_close($curl);

请注意,如果您想查看 Cpanel 作为对

$result
的响应返回的内容,请将
print $result;
放在
curl_close($curl);

之后

0
投票

谢谢 BugLogic

这对我有用,只需在网址上的端口之前添加一个冒号

$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."";

© www.soinside.com 2019 - 2024. All rights reserved.