1

I want to integrate Directadmin CMD_API_SHOW_DOMAINS function, but I can't understand how to explode returned array.

directadmin_api specifications

Currently, I'm using this code:

<?
include("httpsocket.php"); 
$sock = new HTTPSocket; 
$sock->connect('myip',2222); 
$sock->set_login('admin', 'pass123'); 
$user= "username"; //user which own printed domains.
$sock->query('/CMD_API_SHOW_USER_DOMAINS?user='.$user);
$domains = $sock->fetch_parsed_body();
print_r ($domains); // printing fetched array

?>

And I receive this:

Array ( [rara_com] => 0.000000:unlimited:0.0664062:0.000000:0:no:unlimited:OFF:ON:ON ) 

For user x, which has rara.com domain. This api show all domains, so there could be more arrays displayed in page, for example user Y has 3 domains. google.com, yahoo.com, stackoverflow.com, so api will return me result:

Array ( [google_com] => 0.000000:unlimited:0.0664062:0.000000:0:no:unlimited:OFF:ON:ON ) Array ( [yahoo_com] => 0.000000:unlimited:0.0664062:0.000000:0:no:unlimited:OFF:ON:ON ) Array ( [stackoverflow_com] => 0.000000:unlimited:0.0664062:0.000000:0:no:unlimited:OFF:ON:ON )

How explode these arrays? It's really big headache for me. I need only first element (domain name, for example rara_com, google_com)

Directadmin api https://www.directadmin.com/api.php (function CMD_API_SHOW_DOMAINS)

Specifications in the bottom ↓ ↓ ↓

Get User Domains:
Function    Retrieve the list of domains owned by the user, and some basic stats    
Command CMD_API_SHOW_USER_DOMAINS
Method  GET
Success Returns url encoded array
Failure Returns error=1
Form Values:
Name    Value
user    Username of the user for which you wish to view the stats
Array Returns Values
Name    Value
domain.com  Colon speparated list with domain information: eg. 6757.4:unlimited:0.000356674:93.5:2:no:unlimited:ON:ON:ON where the data is bandwidth used:bandwidth limit:disk usage for the domain:log usage for the domain:# of subdomains:suspended:quota:ssl:cgi:php
( domain2.com ... ) Same as domain.com, one entry for each domain
Modus Tollens
  • 5,083
  • 3
  • 38
  • 46

1 Answers1

0

You want to get the results in a foreach loop where you define the result as a key, value pair.

This is the code that i use for doing the same:

$sock = new HTTPSocket;
$sock->connect("ssl://" . $server,2222);
$sock->set_login('foo','bar');
$sock->query('/CMD_API_SHOW_USER_DOMAINS?user=' . $user);
$result = $sock->fetch_parsed_body();


    if(empty($result))
        {
            echo "Problem connecting to server: " . $server;
        }
    else
        {
                        
            foreach($result as $key => $value)
            {
                
                echo 'Domain : ' . $key;
            };  
            
        };  

You will need to string replace _ with a . on your results as the domain values come in with underscores instead of periods in the extension.