The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

programmatically create a mySQL database

Discussion in 'cPanel Developers' started by cneeds, Sep 15, 2010.

  1. cneeds

    cneeds Registered

    Joined:
    Sep 15, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    I have the following code that does not work. If anyone can see the error(s) I would really appreciate you telling me about them :)

    PHP:
    function execCommand($command) {
      global 
    $curl_path;

      if (!empty(
    $curl_path)) {
        return 
    exec("$curl_path '$command'");
      }
      else {
        return 
    file_get_contents($command);
      }
    }

        
    $execute execCommand("http://cneeds:password@inandrias.com:2082/frontend/x3/sql/adddb.html?db=test");

    Note that my cpanel skin is x3.

    I tried with and without the curl path...

    I get this error
    .

    Chris
     
    #1 cneeds, Sep 15, 2010
    Last edited: Sep 15, 2010
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi Chris,

    There are a few ways that you could programmatically create databases. The bests ways utilize the cPanel API1 Mysql module. This is the same module used by the cPanel interface. (Actually, all of the cPanel interface used API functions). If you use this particular API module, you will not only create the actual MySQL database resource, but will also perform other actions in the cPanel backend (that are critical to have a fully functioning MySQL/cPanel account relationship). This 'extra' stuff is mostly relevant only to cPanel versions == 11.25.1 and versions >= 11.27. Using the API over a direct, UI URL GET/POST, as in your example, has a number of benefits:
    Just a few...
    1) safe authentication, using support cPanel authentication methods
    2) if the cPanel interface changes, it's unlikely that the underlining API functionally will NOT change. ie, it's less fragile and longer lived code when you code against an API.

    Supposing you intend to use PHP, you have a couple of options on how to invoke API calls:

    If you are doing in within the context of cPanel, ie, you want to either have a custom page, maybe with a form, or a 3rd party application you could can use either LivePHP or cPanelPHP.

    There's also the XML-API. The XML-API is often used for remote connections but you can use it locally :) There's a great PHP client class for making XML-API requests; it can be found here, http://sdk.cpanel.net/lib/xmlapi/php/cp_xmlapi_php_v1.0.6.tar.gz . It includes a few examples too.

    Most of the forum posts that deal with this topic, of which there are several, use the XML-API; often employing the PHP client class. Probably because it's so utilitarian and ease to code with.

    The XML-API has native functions which provide WHM functionality when authenticated as root or a reseller (with the correct permission for the call in question). It also can perform API1 and API2 functions when authenticated as root (on the user's behalf) or as a cPanel user.

    If I knew the exact context under which you were executing your script, I might be able to better direct you on an implementation method. But since I don't and the XML-API works in nearly every context, for now I'll simply recommend using the XML-API and making the API1 Mysql::adddb call.

    You can find documentation:
    1) on the Mysql module here, ApiMysql < ApiDocs < TWiki

    2) on XML-API here, XML and JSON APIs

    3) there's a blog post I wrote that uses the XML-API to make local API1 Mysql calls after account creation, Frequently Asked Scripts: "How do I automate XYZ after creating an account?" - cPanel Integration . It has alot of good info in it. You should be able to translate that stuff to any script you have in mind.

    Take a look at those links and search around the forum for more recent threads on MySQL automation, database creation, XML-API, and Mysql::adddb.

    I'm happy to answer any questions related to those documents or address any more specific implementation needs you might have.

    Regards,
    -David
     
  3. cneeds

    cneeds Registered

    Joined:
    Sep 15, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Hi David

    Thank you for a brilliantly detailed response which I will go through later.

    Basically I am selling applications to professionals and for each application I need to create a database and relevant tables for the buyer to keep their data in. I don't want all my client's data in one database.

    I will try and get the api calls to work.

    Thanks again.

    Chris
     
  4. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Okay, cool.

    Yeah, in that case I would probably stick with using the XML-API. In fact, I'd extend the PHP client class or make an interface class that uses an extended XML-API PHP client class as an adapter. If you do something like that you isolate your business logic(your website that sells the applications) from the resource creation/allocation. ie, if you decided to move your main site to a different cPanel box, or even into some other control panel/cloud app realm, your underlying logic (that creates these application and database resources) wouldn't change much, if at all. Also, by doing an interface/adapter paradigm, you could have different adapters (aka HTTP/REST/SOAP) that communicate to other cPanel boxes (if there were some fundemental differences, say DB Prefixing off), non-cPanel boxes, or a web service. The interface is simply an enforcement of the calls that must be supported by your adapters (because your app selling routines will simply call 'create_database' on the interface and the underlying adapter will do whatever in necessary and particular for that target platform/control panel).

    Then again, I just might be the guy who abstracts the code too much :p

    Best Regards,
    -David
     
  5. cneeds

    cneeds Registered

    Joined:
    Sep 15, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    No, your ideas are good. I don't know how to do them but I understand the principles with regards to flexibility of account movement. And these issues should be considered upfront, not way down the road when its no longer possible to implement them.

    Chris
     
Loading...

Share This Page