skip to Main Content

With help from Oleg Neumyvakin I have managed to use plesk api to create a database, but now I need to add a user to that database. I have worked out how to add a user to a database,but it requires knowing the id of the database, which I won’t know in php. For instance the following request code creates the database

<packet>
<database>
<add-db>
   <webspace-id>$webspaceId</webspace-id>
   <name>$dbName</name>
   <type>mysql</type>
</add-db>
</database>
</packet>

And the following code creates a user and adds it to the database

<packet>
<database>
  <add-db-user>
      <db-id>26</db-id>
      <login>zebra</login>
      <password>zebra500</password>
   </add-db-user>
</database>
</packet>

You can see my problem. The second part uses the id of the database which is 26 to add the user, but I don’t know the id without looking in plesk, and that defeats the object.

So how do I create the database and add the user at the same time?

2

Answers


  1. Chosen as BEST ANSWER

    I have the answer at last. Although I am not sure if it is the best answer. At the moment it is the only answer. If you can give me a better solution. I will of course tick yours instead. I am getting the id back with the preg match then stripping the id tag and using the id to create a user.

    $request = <<<EOF
    <packet>
    <database>
    
    <add-db>
       <webspace-id>$webspaceId</webspace-id>
       <name>$dbName</name>
       <type>mysql</type>
    </add-db>
    
    </database>
    </packet>
    EOF;
    
    $response = $client->request($request); // Send query to Plesk host
    echo $response; // show response
    
    // This gets the id by using preg match
    preg_match('/<id>(.*)</id>/',$response,$match);
    $databaseId=$match[0];
    echo '</br></br>match:'.($databaseId).'</br></br>';
    // This strips the id tag
    $answer=$match[0];
    $search = array('<id>', '</id>');
    $replace = array("","");
    $id = str_replace($search, $replace, $answer);
    
    $request = <<<EOF
    <packet>
    <database>
    
      <add-db-user>
          <db-id>$id</db-id>
          <login>bert</login>
          <password>bert500</password>
       </add-db-user>
    
    </database>
    </packet>
    EOF;
    
    '</br></br>'.$response = $client->request($request); // Send query to Plesk host
    echo $response; // show response
    

  2. You can use any of PHP built-in XML parsers like SimpleXML or DOMDocument

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search