PHP class for communicating with DA

huglester

Verified User
Joined
Sep 3, 2012
Messages
22
Location
Lithuania
Okay, solved this in the 'right' way.
The class seems to be working without any modifications.

I had slightly miss-configured the Directadmin.
SO I restored old line: $socket = @fsockopen( $this->remote_host, $this->remote_port, $sock_errno, $sock_errstr, 10 );


I copied certificate ROOT BUNDLE (5.58 KB) file from here: https://support.comodo.com/index.php?/Default/Knowledgebase/Article/View/979/108/domain-validation-sha-2 (since I have comodo certificate).
to /usr/local/directadmin/conf/carootcert.pem
and added : carootcert=/usr/local/directadmin/conf/carootcert.pem to directadmin.conf and restart.
 

tcorneli

New member
Joined
Sep 20, 2016
Messages
2
How to use the API?

Hi, I am having serious trouble using the API. I am using the same code as used in the examples on https://www.directadmin.com/api.php but all I am getting is the home page of the user I am trying to do something for, instead of a parseable result:
Code:
[PHP]
require_once('httpsocket.php');
$sock = new HTTPSocket();
$sock->connect("ssl://{$server_name}", $server_port);
$sock->set_login("admin|{$user}", $password);
$sock->query('CMD_API_DATABASES');
$result = $sock->fetch_body();
print $result;
[/PHP]
Any help would be appreciated.
PS: I don't know if this is important, but I am trying to do this locally on the server.
 

vlijmenlive

Verified User
Joined
Nov 13, 2013
Messages
64
Location
Belgium
Hi,

I'm not 100% sure, but shouldn't it be /CMD_API (with the slash)? Besides that you should also get an error value returned that gives you more information.

Regards, Niels
 

MaartenS

Verified User
Joined
May 23, 2013
Messages
7
200 OK, but not OK...

Hi all,

First of all: I love how this thread has been going for years and years. It's provided to be a wealth of information!

I've been working with the DA api class for a while now and managed to get it to work pretty much the way I want to. Until now. Something changed, and I don't have a clue what.

PHP:
$this->query('/CMD_API_CHANGE_DOMAIN',
                array(
                    'old_domain' => $old_domain,
                    'new_domain' => $new_domain
                )
);
Now, this seems like a good query. Yet, the result of this is this:

PHP:
//Dumped from Laravel
"HTTP/1.1 200 OK\r\n"
If I remove the set_method('POST') before the query call, this is the result:
PHP:
"""
HTTP/1.1 200 OK\r\n
Server: DirectAdmin Daemon v1.50.0 Registered to \r\n
Set-Cookie: session=; path=/; expires=Fri, 23 Sep 2016 08:43:28 GMT; HttpOnly\r\n
Connection: close\r\n
Content-Type: text/plain\r\n
\r\n
error=1&text=you forgot to post something&details=
"""
Okay, so it works. Other commands work too and get a proper result. Apart from this one (at the moment, anyway).

The error Laravel throws at me is

PHP:
ErrorException in Directadmin.php line 301:
Undefined offset: 1
Which is the line where the Query-function parses the result into a header and a body.
PHP:
list($this->result_header,$this->result_body) = preg_split("/\r\n\r\n/",$this->result,2);
And I get why it throws the error, seeing as the result doesn't have a body. But I don't seem to figure out why it only gives me a Status 200 OK in return, while it's most definitely NOT okay.

Also, as a proof of life from the DA-server itself: I do get the confirmation in the logs that the request has been received and handled:

PHP:
22/09/2016:10:43:28     ip.add.re.ss GET /CMD_API_CHANGE_DOMAIN HTTP/1.0      admin
Anybody have any tips as to how to get DA to return the proper response?
 

MaartenS

Verified User
Joined
May 23, 2013
Messages
7
Feeling silly...

Okay, that was stupid. I figured out what I did wrong.

Instead of sending through the user name and password, I passed through the admin user and password. Which doesn't work for this specific API call. And which explains why other functions did work, just not this one.

Begs the question: why do I get a HTTP 200 OK response, and not a 'This call does not exist in this userlevel' or something?
 

DirectAdmin Support

Administrator
Staff member
Joined
Feb 27, 2003
Messages
8,923
Okay, that was stupid. I figured out what I did wrong.

Instead of sending through the user name and password, I passed through the admin user and password. Which doesn't work for this specific API call. And which explains why other functions did work, just not this one.

Begs the question: why do I get a HTTP 200 OK response, and not a 'This call does not exist in this userlevel' or something?
Hi Maaren,
I've made change for the next release of DA so it generates a 403 insetad of a 200 when invalid CMD_* or CMD_API_* requests are made.
It will also spit out proper url-encoded API data if CMD_API_ is used:
https://directadmin.com/features.php?id=1885

Available in the pre-release binaries in about 20 minutes, if anyone needs them.

John
 

matjon

New member
Joined
Jun 30, 2018
Messages
1
License

Hello,
What are the copyright terms for this class (and the example source code)? Is it licensed under the MIT, BSD or some other license? I have found no such info, and therefore the license seems to default to "forbidden for all uses" (except perhaps some fair use depending on country), which is probably not what the Author(s) intended.
And who, except from Phi1 Stier, is the Author of this class?

It would be nice to update the source code with respective copyright information.

IANAL

Greetings,
Mateusz
 

DirectAdmin Support

Administrator
Staff member
Joined
Feb 27, 2003
Messages
8,923
Hi Mateusz,

I cannot speak directly for Phil, but I personally have done the 3.x.x updates to the code, to convert it over to curl for better control of headers, redirect, ssl protocols, etc..
I can say that the intent of the class is just to fill the gap between your own code and the DirectAdmin API.. and as that's essentially the only use for it, hence there wasn't much need for licensing at that time.
So if you use it for your DirectAdmin API projects, you won't have issues. If you start to wander copy/pasting the code for non-DA use, I cannot really say, as that wasn't it's intent, but at the end of the day it's Phil's baby ;)

Regardless, I've just sent him an email to see how he wants to proceed, probably a GNU GPL is sufficient, just to add clarification as to how you can go about using it.
But as mentioned, for your DirectAdmin API project, yes please use it :) Beyond that, we'll wait for his reply.

John
 
Top