PHP cPanel创建子域

时间:2017-04-29 03:34:46

标签: php subdomain cpanel

所以我试图使用PHP自动为我的网站创建子域名。我尝试了以下代码,但它给了我def array_purge(array) array.reject! { |item| item.start_with?('@') } end >> array = ['Hello', '123', '@SomeUselessText', 'info@SomeSite.com'] >> array_purge(array) => ["Hello", "123", "info@SomeSite.com"] 错误并将我重定向到我的cPanel登录

301

就像我说它给我一个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而不是仅仅在代码中执行此操作而不是让我手动登录到cPanel。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:3)

解答: 在摆弄我的代码后,我意识到端口2082和端口2083是相同的,只是2082没有https://所以我将端口更改为2082并且它有效!

CODE:

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

答案 1 :(得分:1)

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

但是,将here给出的用于身份验证的代码和here给出的用于添加子域的代码结合在一起,可以得到以下看起来很不错的脚本:

$teamStudents = DB::table('students')->orderBy('name', 'ASC')
    ->leftJoin(DB::raw('(select SUM(products.money * products.quantity) AS products_total, student_id from products group by student_id) products'), 'students.id', '=', 'products.student_id')
    ->leftJoin(DB::raw('(select sum(payments.money) as payments_total, student_id from payments group by student_id) payments'), 'students.id', '=',
        'payments.student_id')
    ->select('students.name', 'payments.payments_total', 'products.products_total', 'students.id AS id')
    ->groupBy('students.id')
    ->get();

结果应该是这样的:

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