使用PHP

时间:2017-06-13 15:09:42

标签: php api cpanel whm

我原本希望这是一个很容易完成的任务,但是现在我已经深入研究了几天,整个过程似乎比我原先想象的要复杂得多。

我想在mydomain.com下创建一个新的子域名,例如test123.mydomain.com,通过WHM API 1或cPanel API 2使用PHP,随后在新的子域下使用新的MySQL数据库。

我假设我需要首先使用WHM进行身份验证(使用WHM远程API令牌?),然后调用相应的API模块和函数,最后检查响应是否成功。由于我是用PHP做的,所以我假设我需要通过curl完成所有操作。在cPanel API 2 docs中,我看到了这个电话:

https://hostname.example.com:2087/cpsess##########/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=subdomain&rootdomain=example.com&dir=%2Fpublic_html%2Fdirectory_name&disallowdot=1

不幸的是,由于我没有打开“cpsess”,因为我通过PHP脚本执行此操作并使用WHM远程替换“cpsess ##########”,因此没有多大帮助API令牌也不起作用。我已经尝试了一些不同的方法,到目前为止我唯一可以工作的是用于在服务器上列出帐户的cPanel API 1示例...它工作得很好,但它在API 1上没有可用于创建子域的模块:

function getUserAccountList($user,$token){
    $userList = "";
    $query = "https://hostname.example.com:2087/json-api/listaccts?api.version=1";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
    $header[0] = "Authorization: whm $user:$token";
    curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
    curl_setopt($curl, CURLOPT_URL, $query);
    $result = curl_exec($curl);
    $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ($http_status != 200) {
        $userList = "[!] Error: " . $http_status . " returned\n";
    } else {
        $json = json_decode($result);
        $userList.= "[+] Current cPanel users on the system:<BR><BR>";
        foreach ($json->{'data'}->{'acct'} as $userdetails) {
            $userList.= $userdetails->{'user'} . "<BR>";
        }
    }
    curl_close($curl);
    return $userList;
}

问题是:如何使用WHM API 1或cPanel API 2使用PHP脚本创建新的子域和MySQL数据库?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我找到了一个非常有效的解决方案,并且可以解决cPanel和WHM可用的不同API版本的大部分猜测:

https://www.codepunker.com/blog/using-php-to-create-new-subdomains-databases-and-email-accounts-on-a-cpanel-driven-server

此设置需要安装由Mochamad Gufron开源的Composer,PHP依赖项管理器和mgufrone / cpanel-whm软件包,因此您需要对服务器进行root访问才能执行此操作。在上面的链接上是获取设置的所有信息,以及通过PHP的WHM / cPanel API创建新的子域和MySQL数据库。