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.

xml-api removeacct error

Discussion in 'cPanel Developers' started by uzor, Jul 14, 2011.

  1. uzor

    uzor Member

    Joined:
    Jul 14, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Hello,
    I can't remove the account using xml-api, however
    creating the account works fine.
    My code:

    PHP:
            $params = array(
                
    'user' => 'wat18848',
            );

            
    $config = array(
                
    'config'  => array(
                    
    'host' => $this->_config['host'],
                    
    'user' => $this->_config['username'],
                    
    'password' => $this->_config['password'],
                ),
            );

            
    $cmd 'removeacct';

            
    $xmlapi = new Cpanel_Service_WHM($config);
            
    $result $xmlapi->$cmd($params)->getResponse('array');
    But I always get
    Code:
    Array
    (
        [result] => Array
            (
                [0] => Array
                    (
                        [status] => 0
                        [statusmsg] => No user specified
                        [rawout] => 
                    )
    
            )
    
    )
    
    However, if I run this command directly from cpanel like this:
    https://<host>/cpsess7719510149/xml-api/removeacct?user=<userid>
    It removes the account.
    So what am I doing wrong?
     
  2. uzor

    uzor Member

    Joined:
    Jul 14, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Nevermind, got it working.. But why doesn't it work if username is passed in an array?
     
  3. 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 uzor,

    Glad to here you got it working.

    The reason is that the Cpanel_Service_WHM class currently uses the PHP XML-API client class for backwards compatibility. That methods in that class ('removeacct', 'createacct', etc) usually take ordered parameters and not an arrays or objects.

    Just so anyone reading this isn't confused, uzor is using the cPanel PHP Library, which is bundled with the PHP PublicAPI class.

    You're completely welcome to use that class directly (it's part of the component-oriented design of that library ;) ). However, you might consider creating your object using the PublicAPI class since it abstracts both that class and the Cpanel_Service_cPanel...just a suggestion...you know what's best for your application :)

    In future versions of that class/library, we hope to add the logic to address such legacy/backwards-compatibility quirks.

    As a side note, I would recommend a slightly different style when retrieving the response result
    PHP:
    $response $xmlapi->$cmd($params);

    if (
    $response->validResponse()) {
        
    // the server response was valid and can be parsed to 'array' or
        //  whatever other supported type you want
        
    $result $response->getResponse('array');
    } else {
        
    // the server response was invalid; you can fetch an array of error messages
        
    $result $response->getResponseErrors();

        
    /**
         * or you might want to throw an exception or trigger a warning
         * -- do error handling appropriate for your application here -- 
         * EX: 
         *  $msg = " -- Server responded with bad data --\n";
         *  $msg .= "Error msgs:\n" .implode( "\n", $result );
         *  $msg .= "\nRAW RESPONSE:\n" .$response->getRawResponse() . "\n";
         *  throw new Exception(  $msg );
         */
    }
    It's a little more code but will protect your application from having unexpected errors if the server sends back a mal-formed result.

    Best Regards,
    -DavidN
     
Loading...

Share This Page