Results 1 to 6 of 6

Thread: xml-api script works only sometimes?

  1. #1
    Member
    Join Date
    Apr 2010
    Posts
    6

    Question xml-api script works only sometimes?

    Hey - So this script works great. Sometimes. About 50% of the time it does not make the database. All the accounts it was tested on are members of the same package

    I am at a loss for what the problem is. I would guess it is something to do with cpanel/whm and whmcs being stupid but, I just want to run this by people who know more about this sort of than than myself.

    Also: Maybe the code can help someone trying to figure out what to do with the xmlapi.php class (Call me slow but I had no idea what to do with it at first)

    NOTE: This is a partial class but, all the stuff relating to the database creation is shown. The rest is just custom stuff relating to my application
    PHP Code:

    require("xmlapi.php");
    $installer = new installer();
    if (
    $installer->pageCheck() != "1") {
        die(
    "URL not authorized.");
    }

    $installer->makeDatabase();            // Creates the database. -> Disabled for testing so the db info remains constant.
    $installer->writeDatabase();         // Makes the database.
    $installer->makeconfig();            // Writes the config file.

    class installer {
        var 
    $ip "127.0.0.1";
        var 
    $root_pass "*****";
        var 
    $database "cs2";
        var 
    $username;
        var 
    $password;
        var 
    $sqluser;
        var 
    $sqldir "sql/";
        var 
    $domain;
        
        function 
    __construct() {
            
    /*
             * Constructor!
             */
            
    $this->makeUser(); // Create the user!
            
    $this->makePassword(); // Create the password!
        
    }
        public function 
    pageCheck() {
            
    /*
             * pageCheck checks if it is a valid account to run the install on.
             */
            
    $xmlapi = new xmlapi($this->ip);
            
    $xmlapi->password_auth("root",$this->root_pass);
            
    $xmlapi->return_xml(1);
            
    $xmlapi->set_debug(1);
            
            
    $domain $_SERVER['HTTP_HOST'];
            
    $pieces explode("."$domain);
            if (
    $pieces[0] == "www") {
                unset(
    $pieces[0]);
                
    $domain implode(""$pieces);
            } else {
                
    $domain implode(""$pieces);
            }
            
    $username substr($domain07);
            
    $this->username $username// this field is now the first 7 characters of the URL it is the username.
            
            
    $status "0"// Default of status
            // get list of accounts
            
    $accounts $xmlapi->listaccts();
            
    // create SimpleXML object to loop through later
            
    $xml2 = new SimpleXMLElement($accounts);
            foreach (
    $xml2->acct as $account) {
            
    $myuser $account->user;
                
    // echo $myuser."<br />";
                
    if ($myuser == $this->username) {    // This is to see if the username matchs a real account
                    
    $status "1"// valid account.
                
    }
            }
            return 
    "1";
        }
        public function 
    makeDatabase() {
            
    /*
             * this is the database making stuff
             */
            
    $xmlapi = new xmlapi($this->ip);
            
    $xmlapi->password_auth("root",$this->root_pass);
            
    $xmlapi->return_xml(1);
            
    $xmlapi->set_debug(1);
            
            
    $xmlapi->api1_query("$this->username""Mysql""adddb", array($this->database) );
            
    $xmlapi->api1_query("$this->username""Mysql""adduser", array( $this->sqluser$this->password ) );
            
    $xmlapi->api1_query("$this->username""Mysql""adduserdb", array($this->database$this->sqluser'ALL' ));  
        }


  2. #2
    Member
    Join Date
    May 2008
    Posts
    30

    Default

    Hi,

    If it's only working sometimes, then your basic code must be okay, but you simply aren't handling errors.

    A few suggestions:

    1. Trap error messages, perhaps put them in a log file.

    2. Check /usr/local/cpanel/logs/error_log when it fails for an error message.

    3. Check out Matt's tool for logging API calls:

    http://forums.cpanel.net/f42/looking...rs-130545.html

    I find it infinitely useful for debugging calls.

  3. #3
    Member
    Join Date
    Apr 2010
    Posts
    6

    Default

    actually. you will love this.
    subdomain.domain.com for example :
    if the subdomain is <= 7 it works
    if the subdomain is > 7 it doesn't

    so im guessing it is generating a messed up username or something if subs are over 7.

    ...more to come soon.

  4. #4
    Member
    Join Date
    Apr 2010
    Posts
    6

    Default

    OK so. fixed it. all the code at the top remains the same except for this function:


    PHP Code:
        public function pageCheck() {
            
    /*
             * pageCheck checks if it is a valid account to run the install on.
             */
            
    $xmlapi = new xmlapi($this->ip);
            
    $xmlapi->password_auth("root",$this->root_pass);
            
    $xmlapi->return_xml(1);
            
    $xmlapi->set_debug(1);
            
            
    $domain $_SERVER['HTTP_HOST'];
            
    $pieces explode("."$domain);
            if (
    $pieces[0] == "www") {
                unset(
    $pieces[0]);
                
    $domain implode(""$pieces);
            } else {
                
    $domain implode(""$pieces);
            }
            
    $username substr($domain07);
            
    // $this->username = $username; // this field is now the first 7 characters of the URL it is the username.
            
            // get list of accounts
            
    $accounts $xmlapi->listaccts();
            
    // create SimpleXML object to loop through later
            
    $xml2 = new SimpleXMLElement($accounts);
            foreach (
    $xml2->acct as $account) {
            
    $myuser $account->user;
                if (
    strlen($myuser) > 7) {
                    
    $myuser substr($myuser07);    
                }
                 
    // echo $myuser." ".strlen($myuser)."<br />";
                
    if ($myuser == $username) {    // This is to see if the username matchs a real account
                    // echo "ACCOUNT: ".$account->user."<br />";
                    
    $this->username $account->user;
                    
    $status "1"// valid account.
                
    }
            }
            return 
    "1";
        } 

  5. #5
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    570

    Default

    I would imagine it has something to do with the user name logic.
    Code:
    if ($pieces[0] == "www") { 
                unset($pieces[0]); 
                $domain = implode("", $pieces); 
            } else { 
                $domain = implode("", $pieces); 
            } 
    $username = substr($domain, 0, 7);
    I could be totally wrong that this is the source, though a few debug statements in your various methods should show exactly what's going on and why the variable contents aren't jiving with WHM


    It would be better if you used the listaccts method to get the username; utilizing the searchtype and searchterm arguments

    Code:
    /**
            * List accounts on Server
            *
            * This call will return a list of account on a server, either no parameters or both parameters may be passed to this function.
            *
            * @param string $searchtype Type of account search to use, allowed values: domain, owner, user, ip or package
            * @param string $search the string to search against
            * @return mixed
            * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListAccounts XML API Call documentation
            */
            public function listaccts($searchtype = null, $search = null) {
    ListAccounts < AllDocumentation/AutomationIntegration < TWiki

    If the domain is valid, you'll get a response that contains the username.

    Even if this doesn't solve your immediate problem, it's a more accurate way to get the username for a domain. You can't always rely on the username being the first 8 characters of the domain name, though that is the current default form value in WHM and may be your practice.

    Regards,
    -Dave
    David Neimeyer
    Integration Developer

    sdk.cpanel.net
    APIs: XML-API API1 & API2
    Check Out: Developer Downloads Integration Blog
    Need Support? Support Ticket Developer Forum Feature Requests

  6. #6
    Member
    Join Date
    Apr 2010
    Posts
    6

    Default

    ya the issue is that is was making 7 character usernames if the chosen subdomain was below 8 characters and 8 character usernames if the chosen subdomain was 8 or above characters.

    the second version i posted fixes this by checking the first 7 characters of the url against the first 7 of the username and when it finds one that matches it uses the full length username supplied by the xml-api to create the databases.

    but anyway it works now for any length username on my system so i am happy =P

Similar Threads

  1. Cannot Create New Email Accounts with XML API Script
    By christsealed in forum E-mail Discussions
    Replies: 1
    Last Post: 04-30-2011, 06:48 PM
  2. XML API class works with a WHM cPanel reseller account?
    By bcosmo in forum cPanel Developers
    Replies: 1
    Last Post: 03-17-2010, 09:33 AM
  3. XML-API and controling WHM using remote Perl script - Help!
    By jonathanwww in forum cPanel Developers
    Replies: 1
    Last Post: 06-30-2008, 10:09 AM
  4. XML API: always get login screen, not XML
    By sldff3ald in forum cPanel Developers
    Replies: 3
    Last Post: 09-19-2007, 09:29 AM
  5. How do I use the xml - api?
    By rhopperger in forum cPanel Developers
    Replies: 3
    Last Post: 05-19-2007, 08:07 AM