PHP / Cpanel - 如何在没有第三方应用程序的情况下创建cPanel帐户?

时间:2017-01-18 01:36:28

标签: php automation cpanel whm

晚上SO社区,

我一直在试图弄清楚如何使用PHP动态创建cPanel帐户。在进行我的问前研究时,我注意到这个问题被问过很多次,并得到了一些答案。不幸的是,我发现的答案似乎已在最新的cPanel版本中折旧。我一直试图让下面的脚本工作,但没有运气。 注意:我没有创建脚本,我只是忘记了源。如果你知道用托管包创建cPanel帐户的任何方法,请告诉我!

names = {student.name};
gpas = [student.name];

如果您需要更多信息,请与我们联系!

提前谢谢你, 添

EIDT 资料来源:https://github.com/Ephygtz/cPanel-WHM-automated-account-creator/blob/master/create-whm-account.php

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

下午好,

我知道这些问题有点陈旧,但我找到了一个适合我的解决方案。请参阅下文。

<?php
    Function createAccount ($newDomain = "", $newUser = "", $newPass = "", $newPlan = "", $newEmail = "") {
        $whm_user = "usename";
        $whm_pass = "password";
        $whm_host = "example.com";

        $newDomain = fixData($newDomain);
        $newUser = fixData($newUser);
        $newPlan = fixData($newPlan);

        If (!empty($newUser)) {
            // Get new session ID
            $sessID = newSession($whm_host, $whm_user, $whm_pass);

            $script = "https://www." . $whm_host . ":2087" . $sessID . "/scripts/wwwacct";
            $sData = "?plan={$newPlan}&domain={$newDomain}&username={$newUser}&password={$newPass}&contactemail={$newEmail}";

            // Authenticate User for WHM access
            $context = stream_context_create(array(
                'http' => array(
                    'header' => "Authorization: Basic " . base64_encode("{$whm_user}:{$whm_pass}"),
                ),
            ));

            // Get result
            $result = file_get_contents($script.$sData, false, $context);
            //Echo "URL: " . $script.$sData . "<br /><br />";
            // Echo "Result: " . $result;

        } Else {
            Echo "Error: Username is empty!";
        }
    }


    Function newSession($nHost, $nUser, $nPass) { // Example details
        $ip = $nHost;
        $cp_user = $nUser;
        $cp_pwd = $nPass;
        $url = "https://$ip:2087/login";

        // Create new curl handle
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$cp_user&pass=$cp_pwd");
        curl_setopt($ch, CURLOPT_TIMEOUT, 100020);

        // Execute the curl handle and fetch info then close streams.
        $f = curl_exec($ch);
        $h = curl_getinfo($ch);
        curl_close($ch);

        // If we had no issues then try to fetch the cpsess
        if ($f == true and strpos($h['url'],"cpsess"))
        {
            // Get the cpsess part of the url
            $pattern="/.*?(\/cpsess.*?)\/.*?/is";
            $preg_res=preg_match($pattern,$h['url'],$cpsess);
        }

        // If we have a session then return it otherwise return empty string
        return (isset($cpsess[1])) ? $cpsess[1] : "";
    }

    Function fixData($data) {
        $data = str_replace("-","%2D",$data);
        $data = str_replace(".","%2E",$data);
        $data = str_replace(" ","%20",$data);

        return $data;
    }

    Function returnData($data) {
        $data = str_replace("%2D","-",$data);
        $data = str_replace("%2E",".",$data);
        $data = str_replace("%20"," ",$data);

        return $data;
    }
    header('Location: https://www.example.com/users');
?>