PDA

View Full Version : PHP <=> DirectAdmin's API



Eric
12-22-2005, 02:06 AM
/*
* 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

hehachris
12-22-2005, 11:20 AM
wts the difference compare with the sticky one?

Eric
12-22-2005, 12:45 PM
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.

interfasys
12-23-2005, 12:45 AM
Thank you for sharing!

Eric
12-23-2005, 08:59 AM
You're welcome. :)

Barty
12-31-2005, 08:00 AM
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 ;)

jmstacey
12-31-2005, 05:11 PM
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)

Eric
12-31-2005, 07:21 PM
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

Barty
01-01-2006, 11:35 AM
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?

Eric
01-01-2006, 03:54 PM
The API command to get all users under a reseller is CMD_API_SHOW_USERS, which is provided in Example.php in method getUsers().

Barty
01-01-2006, 11:38 PM
Ok sorry. But same result with the API command. Only get to see the login page.

Eric
01-02-2006, 05:19 AM
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
header('Content-Type: text/plain; charset=utf-8');
require './DirectAdmin.php';
require './Example.php';
$da = new Example('http://admin:password@domain.tld:2222');
$statistics = urldecode($da->retrieve(array('command' => 'CMD_API_ADMIN_STATS')));
parse_str($statistics , $result);
print_r($result);
?>

Barty
01-02-2006, 05:31 AM
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>
)

Eric
01-02-2006, 05:43 AM
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.

Barty
01-02-2006, 09:44 AM
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.

Eric
01-02-2006, 11:33 AM
:eek:
I apologize; nothing is misconfigured on your end. ;)

Skinza
01-05-2006, 01:09 PM
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)


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:


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, bas@2tci.nl

- check if it's working.

I hope this is the solution in this case

Please use the fixed attachment above!

Eric
01-05-2006, 05:51 PM
Thanks for catching that, Skinza.

Barty
02-13-2006, 06:14 AM
For future reference; This bug should be fixed in the next release(1.263):

http://www.directadmin.com/features.php?id=616

ekapek
12-16-2009, 04:58 AM
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