PHP <=> DirectAdmin's API

Eric

Verified User
Joined
Mar 25, 2004
Messages
116
PHP:
/*
 * DirectAdmin.php:  The base class.
 * Example.php:  The subclass.
 * index.php:  The main script.
 * index.xsl:  The stylesheet.
 */

The base class (DirectAdmin.php) provides an API for the subclass (Example.php) to retrieve data from DirectAdmin's API.

The subclass (Example.php) contains a set of methods/functions that wraps DirectAdmin's commands and parses the output from DirectAdmin's API.

The main script (index.php) formats the data returned to XML, HTML, etc.

The stylesheet (index.xsl) can be linked to a XML document to output text/html/xhtml.

DirectAdmin.php has been updated. - 2006-01-05
 

Attachments

  • da.zip
    3.6 KB · Views: 2,778
Last edited:
I haven't had a chance to study the one that Phi1 Stier wrote, so I can't answer that. I originally wrote this for my own to do billing and invoicing. I thought I'd share it.
I suppose the purpose is the same; that is, pulling data from DirectAdmin's API and format it with PHP. The approaches might be different.
 
Last edited:
I seem to be unable to use this script, or the other one to use the API.

I never get any data from the queries, tried multiple domains and users, never get anything...

Does the API function have to be enabled in DA somehow? Are there some checks I can do to see where the problem lies?

Thanks in advance for any tips!

Ps: Happy holidays ;)
 
make sure error reporting is enabled (E_ALL).

There are not settings within DirectAdmin that can disable the API that I am aware of. Do you get return if you try runing an API command to directadmin through your webbrowser? (It should work)
 
Also try turning on display_errors and display_startup_errors.

fsockopen & fread errors are muted; to show the errors, remove the @ operator on line 58 & 72 in DirectAdmin.php
 
Visiting http://admin:password@domain:2222/CMD_ALL_USER_SHOW just returns me to the login page.

(replacing password and domain with the correct values)

Using the script by Eric, i get the error:
Warning: Invalid argument supplied for foreach() in ... ... ...

Just means that it didn't get any info afaik, right?
 
Last edited:
The API command to get all users under a reseller is CMD_API_SHOW_USERS, which is provided in Example.php in method getUsers().
 
Ok sorry. But same result with the API command. Only get to see the login page.
 
That's probably because username:password is not base64 encoded when you access the API from the browser.

Create a file called test.php and paste the following code into it.
PHP:
<?php
header('Content-Type: text/plain; charset=utf-8');
require './DirectAdmin.php';
require './Example.php';
$da = new Example('http://admin:[email protected]:2222');
$statistics = urldecode($da->retrieve(array('command' => 'CMD_API_ADMIN_STATS')));
parse_str($statistics , $result);
print_r($result);
?>
 
Do I have to encode my user and password myself? Or does the script do that?

Just get the login page using your script:

Array
(
[<html>
<head>
<title>DirectAdmin_Login</title>
<style>
*{_FONT-SIZE:_8pt;_FONT-FAMILY:_verdana;_}_b_{_FONT-WEIGHT:_bold;_}__listtitle_{_BACKGROUND:_#425984;_COLOR:_#EEEEEE;_white-space:_nowrap;_}_td_list_{_BACKGROUND:_#EEEEEE;_white-space:_nowrap;_}_</style>
</head>
<body_onLoad] => \"document.form.username.focus();\">
<center><br><br><br><br>
<h1>DirectAdmin Login Page</h1>
<table cellspacing=1 cellpadding=5>
<tr>
<td class=listtitle colspan=2>Please enter your Username and Password</td></tr>
<form action=\"/CMD_LOGIN\" method=\"POST\" name=\"form\">
<input type=hidden name=referer value=\"/CMD
[#95API] =>
[#95ADMIN] =>
[#95STATS] =>
[#63">
<tr><td_class] => list align=right>Username:</td><td class=list><input type=text name=username></td></tr>
<tr><td class=list align=right>Password:</td><td class=list><input type=password name=password></td></tr>
<tr><td class=listtitle align=right colspan=2><input type=submit value=\'Login\'></td></tr>
</form>
</table>
</center></body></html>
)
 
The script takes care of base64 encoding.
To further help you, I'd need access to your server. Since you are unable to use neither Phi1 Stier's script nor mine, something obviously is misconfigured on your end.
 
Thanks for the help Eric and Jmstacey, I think i have located the problem.

I have a 22char password for my admin account, changing that to a shorter one solved the problem (changing it back to 22 gave api problems again).

I've reported the problem to DirectAdmin support.
 
:eek:
I apologize; nothing is misconfigured on your end. ;)
 
Last edited:
I think i have found the solution for the problem being redirected to the login page. This is because the script does not use an ssl connection to connect to the server. In that case the directadmin service wil reply with the location of the https login page.

Solution:

- Open the file DirectAdmin.php.
- Search for this piece of code (line 53-56)
PHP:
if (!$this->da['port']) {
                $this->da['port'] = ($this->da['scheme'] == 'https') ? 443 : 80;
                $prefix = 'ssl://';
}
- insert the following code after line 56 so the result will be like this:
PHP:
if (!$this->da['port']) {
                $this->da['port'] = ($this->da['scheme'] == 'https') ? 443 : 80;
                $prefix = 'ssl://';
}
if($this->da['scheme'] == 'https') $prefix= 'ssl://'; // fix by Bas van Elburg, [email][email protected][/email]

- check if it's working.

I hope this is the solution in this case

Please use the fixed attachment above!
 
Last edited:
I try this version and previus and always I just have login page in result. I try on 3 servers (don't work) and at one where script work. What is wrong with these 3 servers? Changing passwords doesn't bring success
 
Back
Top