2017-06-13 114 views
0

我曾希望這是一個容易執行的任務,但現在我已經深入研究了幾天,整個過程似乎更多比我原先想象的複雜。通過WHM或cPanel API創建一個新的子域和MySQL數據庫與PHP

我想在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腳本這樣做的,取代了「cpsess ##########「與WHM遠程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; 
} 

的問題是:如何創建一個新的子域和MySQL數據庫的使用無論是南國API 1的cPanel API 2 PHP腳本?任何幫助將不勝感激!

回答

1

我發現它工作得非常好,並採取最猜測出來處理提供給cPanel和WHM不同API版本的解決方案:

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

此安裝程序需要安裝作曲,PHP的依賴經理以及由Mochamad Gufron開源的mgufrone/cpanel-whm包,所以您需要root權限才能訪問服務器。在上面的鏈接中提供了所有需要設置的信息,以及通過使用PHP的WHM/cPanel API創建新的子域和MySQL數據庫。