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 accessing response data

Discussion in 'cPanel Developers' started by N1ghteyes, Mar 3, 2014.

  1. N1ghteyes

    N1ghteyes Active Member

    Joined:
    Mar 3, 2014
    Messages:
    33
    Likes Received:
    2
    Trophy Points:
    8
    Location:
    Reading, UK
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hi Guys,

    Im pretty sure im having a slow day but i cant for the life of me seem to get the data out of the responce object.

    PHP:
    print "<pre>";
    print_r($xmlapi->accountsummary('username'));
    print 
    "</pre>";
    spits out something like this:

    Code:
    SimpleXMLElement Object
    (
        [acct] => SimpleXMLElement Object
            (
                [backup] => 0
                [disklimit] => 2000M
                [diskused] => 372M
                [domain] => ***
                [email] => ***
                [ip] => ***
                [is_locked] => 0
                [legacy_backup] => 1
                [max_defer_fail_percentage] => unlimited
                [max_email_per_hour] => unlimited
                [maxaddons] => *unknown*
                [maxftp] => unlimited
                [maxlst] => unlimited
                [maxparked] => 2
                [maxpop] => unlimited
                [maxsql] => unlimited
                [maxsub] => unlimited
                [min_defer_fail_to_trigger_protection] => 5
                [owner] => ***
                [partition] => home
                [plan] => Standard
                [shell] => /usr/local/cpanel/bin/noshell
                [startdate] => 14 Feb 14 15:58
                [suspended] => 0
                [suspendreason] => not suspended
                [suspendtime] => SimpleXMLElement Object
                    (
                    )
    
                [theme] => x3
                [unix_startdate] => 1392393516
                [user] => ***
            )
    
        [status] => 1
        [statusmsg] => Ok
    )
    however, i cant access any of those in PHP.

    i've tried all sorts, from accessing the properties directly:

    PHP:
    print($username->acct->user);
    to looping over it:

    PHP:
    foreach($ar13bakk->acct as $key => $account){
            
    print_r($account->user);
        }
    which gives:

    Code:
    SimpleXMLElement Object
    (
        [0] => ar13bakk
    )
    
    and i still cant access that. I've tried looping over it, i've also tried accessing the object key ($account->user{'0'}) no joy ether.

    So, what on earth am i missing?
     
  2. N1ghteyes

    N1ghteyes Active Member

    Joined:
    Mar 3, 2014
    Messages:
    33
    Likes Received:
    2
    Trophy Points:
    8
    Location:
    Reading, UK
    cPanel Access Level:
    Root Administrator
    Twitter:
    right.

    After much messing about i descovered why this wasnt working. The data inside the object is also stored as an object. so for example, inside the origonal print_t() we had this:

    Code:
    SimpleXMLElement Object
    (
        [acct] => SimpleXMLElement Object
            (
                [backup] => 0
                [disklimit] => 2000M
                [diskused] => 372M
                [domain] => ***
                [email] => ***
                [ip] => ***
                [is_locked] => 0
                [legacy_backup] => 1
                [max_defer_fail_percentage] => unlimited
                [max_email_per_hour] => unlimited
                [maxaddons] => *unknown*
                [maxftp] => unlimited
                [maxlst] => unlimited
                [maxparked] => 2
                [maxpop] => unlimited
                [maxsql] => unlimited
                [maxsub] => unlimited
                [min_defer_fail_to_trigger_protection] => 5
                [owner] => ***
                [partition] => home
                [plan] => Standard
                [shell] => /usr/local/cpanel/bin/noshell
                [startdate] => 14 Feb 14 15:58
                [suspended] => 0
                [suspendreason] => not suspended
                [suspendtime] => SimpleXMLElement Object
                    (
                    )
    
                [theme] => x3
                [unix_startdate] => 1392393516
                [user] => ***
            )
    
        [status] => 1
        [statusmsg] => Ok
    )
    
    so the structure would normally be something like $account(object)->acct(object)->disklimit(object)->2000M(string)

    infact whats happening is that the values are also objects. The end result is that when attempting to access those values you instead key a key'd object, hence the:

    Code:
    SimpleXMLElement Object
    (
        [0] => ar13bakk
    )
    
    to resolve the issue for now you can typecast with

    PHP:
    $disklimit = (string)$account->acct->disklimit;
    hardly ideal but it works for now. If i get a chance, i'll see about posting a more perminent solution
     
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    648
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
Loading...

Share This Page