Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

[Resolved] Use api 2 call to get user information and convert it to php variable

Discussion in 'cPanel Developers' started by davidhogan, Mar 10, 2014.

  1. davidhogan

    davidhogan Registered

    Joined:
    Mar 10, 2014
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hi,

    I'm currently trying to get user information via whm's xmlapi php class and convert the information into php variables. My code looks like this

    PHP:
    //Ive deleted any information that could be used to login to my server 
    $server_username ='root';
    $ip '0.0.0.0';
    $root_hash 'hash goes here';
    //server ip is passed
    $xmlapi = new xmlapi($ip);
    //server username normally root and hash is passed 
    $xmlapi->hash_auth($server_username,$root_hash);

    $whm_username 'some_user';


    $xmlapi->set_debug(1);


    $args = array('display' => 'bandwidthusage');
    $xmlapi->api2_query($whm_username'StatsBar''stat'$args);

    thank you in advance for your assistance and suggestions

    David
     
  2. vanessa

    vanessa Well-Known Member
    PartnerNOC

    Joined:
    Sep 26, 2006
    Messages:
    830
    Likes Received:
    26
    Trophy Points:
    178
    Location:
    Virginia Beach, VA
    cPanel Access Level:
    DataCenter Provider
    Just set the result to a variable:

    Code:
    <?php
    include("lib/xmlapi.php");
    
    // Variables
    $server_username ='root';
    $cpanel_username =  'cpaneluser'; // Note that this is the cpanel user the API2 call is running against.
    $ip = 'ip';
    $root_hash = 'roothash';
    
    // Call API
    $xmlapi = new xmlapi($ip);
    $xmlapi->hash_auth($server_username, $root_hash);
    $xmlapi->set_debug(0);
    
    $args = array('display' => 'bandwidthusage');
    $result = $xmlapi->api2_query($cpanel_username, 'StatsBar', 'stat', $args);
    
    print_r($result);
    
    
    This will return:

    Code:
    [SimpleXMLElement Object
    (
        [apiversion] => 2
        [data] => SimpleXMLElement Object
            (
                [name] => bandwidthusage
                [_count] => 8.97
                [_max] => unlimited
                [_maxed] => 0
                [count] => 8.97
                [feature] => bandwidth
                [id] => bandwidthusage
                [item] => Monthly Bandwidth Transfer
                [langkey] => INDXBandwidth
                [max] => unlimited
                [module] => Stats
                [normalized] => 1
                [percent] => 0
                [percent10] => 0
                [percent20] => 0
                [percent5] => 0
                [units] => MB
                [zeroisunlimited] => 1
            )
    
        [event] => SimpleXMLElement Object
            (
                [result] => 1
            )
    
        [func] => stat
        [module] => StatsBar
    )
    
    Then all you have to do is pull out the variables you want. For example, to get the value of _max in the above result:

    Code:
    $max = $result->data->_max;
    
    Will return:

    Code:
    unlimited
    
    Hope this helps.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    44,367
    Likes Received:
    1,856
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello :)

    The previous post seems to accurately answer your question so I am marking this thread as resolved. Feel free to update this thread if you have any additional questions.

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice