簡體   English   中英

無法使用 xmlapi 在 cpanel 中創建數據庫

[英]Unable to create a database in cpanel using xmlapi

我正在嘗試使用包含 xmlapi 的 php 腳本創建一個新數據庫。 我按照這里提到的步驟和用法進行操作。 不過,我無法創建它。 在檢查生成的錯誤日志時,我發現了下面提到的錯誤:

 <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="google" content="notranslate" /> <meta name="apple-itunes-app" content="app-id=1188352635" /> <title>Error</title> <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAICAAAAEAIADSAgAAFgAAAIlQTkcNChoKAAAADUlIRFIAAAAgAAAAIAgGAAAAc3p69AAAAplJREFUWIXt1j2IHGUYB/DfOzdnjIKFkECIVWIKvUFsIkRExa9KJCLaWAgWJx4DilZWgpDDiI0wiViIoGATP1CCEDYHSeCwUBBkgiiKURQJFiLo4d0eOxYzC8nsO9m9XcXC+8MW+3z+9/l6l2383xH+iSBpElyTdoda26xsDqp/h0CVZ3vwKm7tMBngAs7h7eRYebG6hMtMBHbMBX89vfARHprQ5U8cwdFQlIOZCVR5di1+w/wWXT/EY6EoN5NZCODuKZLDwzgSMCuBe2fwfX6QZwtpWzqfBBtLC3txF/ZhxKbBGx0EfsTJS77vwmGjlZrD4mUzUOXZjVjGI65cnTXchB8iupdDUb7QinsQZ7GzZftdQj2JVZ49iC/w6JjksIo7OnS9tiA5Vn6GtyK2+1MY5NkhfGDygVrBAxH5WkPuMjR7/3UsUFLl2Q68s4XkA3ws3v9zoSjX28Kr5wL1xrTxa6ou+f6OZGvqPg9v1wZeaUjcELE/DVfNhWFSvy/enOIZ9eq1sTokEMNLWI79oirP8g6fXpVnh7GEvY1sV/OJ4f0UhyKKk6EoX4x5pEkgXv6L6OM99YqNw/c4kXSwG5nkIfpLCynuiahW1GWeJHkfT4aiXO9atz1XcD6I6yLyHu6bIPk6Hg9FeYZ63y9EjBarPDvQ8VJ1nd9V3D4m+RncForyxFCQ4hSeahlej88Hefauurdwaufr5z/F/ZHAX6nL+mZE18e36IWiHLkFocqzW9QXcNz1+wUHxJ/f10JRPjvGP4pk/vj5L3F8AtufdD+/p6dJDknzX+05fDLGtife/766t9MRgFCUffWTudwE3AqBlVCUf0xLYGTQqzzbhydwJ3Y34g318J1tmX+DPBTlz9MS2MY2/nP8DTGaqeTDf30rAAAAAElFTkSuQmCC" type="image/x-icon" /> <!-- EXTERNAL CSS --> <link href="/cPanel_magic_revision_1386192030/unprotected/cpanel/fonts/open_sans/open_sans.min.css" rel="stylesheet" type="text/css" /> <link href="/cPanel_magic_revision_1542513958/unprotected/cpanel/style_v2_optimized.css" rel="stylesheet" type="text/css" /> <style type="text/css"> /* This css is included in the base template in case the css cannot be loaded because of access restrictions If this css is updated, please update securitypolicy_header.html.tmpl as well */ .copyright { background: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzNTlwdCIgaGVpZ2h0PSIzMjAiIHZpZXdCb3g9IjAgMCAzNTkgMjQwIj48ZGVmcz48Y2xpcFBhdGggaWQ9ImEiPjxwYXRoIGQ9Ik0xMjMgMGgyMzUuMzd2MjQwSDEyM3ptMCAwIi8+PC9jbGlwUGF0aD48L2RlZnM+++PC9nPjwvc3ZnPgo=) no-repeat scroll center top transparent; background-size: 25px auto; } </style> <!--[if IE 6]> <style type="text/css"> img { behavior: url(/cPanel_magic_revision_1352233599/unprotected/cp_pngbehavior_login.htc); } </style> <![endif]--> <script> window.DOM = { get: function(id) { return document.getElementById(id) } }; </script> </head> <body class=""> <p class="logo"><img class="main-logo" src="/cPanel_magic_revision_1457841996/unprotected/cpanel/images/cpanel-logo.svg" alt="logo"></p> <div class="error_notice"> <div id="error-wrapper"> <div id="error_msg_contents"> <h2>HTTP error 404</h2> <p>The requested page was not found.</p> <h2>Possible reasons why you are seeing this page:</h2> <ul> <li>A bookmarked URL may have changed since you last visited.</li> <li>The URL was entered incorrectly.</li> <li>The URL was entered with inaccurate capitalization (URLs are <a href="http://wikipedia.org/wiki/Case_sensitivity">case sensitive</a>).</li> </ul> <p>Please re-check the URL you are trying to reach. (<a href="javascript:history.back()">Go Back</a>)</p> </div> </div> </div> <div class="copyright">Copyright© 2019 cPanel, Inc. <br /><a href="https://go.cpanel.net/privacy" target="_blank">Privacy Policy</a></div> </body> </html>

我的php代碼片段如下:

 <?php require("../libs/xmlapi.php"); // this can be downlaoded from https://github.com/CpanelInc/xmlapi-php/blob/master/xmlapi.php class CpanelOperation { private $cpanelCred = array("url"=>"localhost","port"=>2083,"user"=>"myCpanelUsername", "pwd"=>"MyCpanelpwd"); private $xmlapi; private $dbUser = array("userName"=>"dbUserName","pwd"=>"dbuserPwd"); // The user already exists and has access to other DB's public function __construct(){ $this->xmlapi = new xmlapi($this->cpanelCred['url']); $this->xmlapi->set_port($this->cpanelCred['port']); $this->xmlapi->password_auth("".$this->cpanelCred['user']."","".$this->cpanelCred['pwd'].""); $this->xmlapi->set_debug(1);//output actions in the error log 1 for true and 0 false } public function createDB($dbName){ //create database $createdb = $this->xmlapi->api1_query($this->cpanelCred['user'], "Mysql", "adddb", array($dbName)); if($createdb){ $user = $this->assignUser($dbName); if($user){ return true; } else{ echo "User not assgned to the DB"; } } else{ echo "Unable to create db in cpanel again!"; } } public function assignUser($dbName){ $dbNameInCPanel = $this->cpanelCred['user']."_".$dbName; $userInCPanel = $this->cpanelCred['user']."_".$this->dbUser['userName']; //add user $addusr = $this->xmlapi->api1_query($this->cpanelCred['user'], "Mysql", "adduserdb", array($dbNameInCPanel, $userInCPanel, 'all')); } } ?>

關於如何解決這個問題的任何線索?

提前致謝! 我是 PHP 新手,所以如果我聽起來很傻,請多多包涵!

好吧..所以經過大量的研究、試驗和點擊后,我終於設法解決了這個問題。 我將解決方案放在這里,以便它可以幫助面臨類似問題的人。

我沒有使用 xmlapi,而是使用了 uapi,以下代碼片段將創建一個數據庫並授予用戶訪問權限。

 <?php require("../libs/cpaneluapi.class.php"); class CpanelUAPIOperation { private $cpanelCred = array("url"=>"localhost","port"=>2087,"user"=>"cpanelUser", "pwd"=>"cpanelPwd"); private $dbUser = array("userName"=>"dbUser","pwd"=>"dbpwd"); private $cpanel; public function __construct(){ $this->cpanel = new cpanelAPI($this->cpanelCred['user'], $this->cpanelCred['pwd'], $this->cpanelCred['url']); //instantiate the object } public function createDB($dbName, $crud){ $dbNameInCPanel = $this->cpanelCred['user']."_".$dbName; $createDB = $this->cpanel->uapi->Mysql->create_database(array('name' => $dbNameInCPanel)); //Create the database if($createDB){ $databaseuser = $this->cpanelCred['user']."_".$this->dbUser['userName']; // Assign the user to have access to the database. $access = $this->cpanel->uapi->set_privileges_on_database(array('user' => $databaseuser, 'database' => $dbNameInCPanel, 'privileges' => 'ALL')); if($access){ $crud->switchDB($dbNameInCPanel); return true; } } return false; } } ?>

差不多就是這樣! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM