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

davidhogan

Registered
Mar 10, 2014
1
0
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
 

vanessa

Well-Known Member
PartnerNOC
Sep 26, 2006
833
28
178
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.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,913
2,201
363
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.