PHP class for communicating with DA

How to create user "testuser" via reseller "testreseller" for example?
 
Refrence: http://directadmin.com/api.html#create

PHP:
<?php

include 'httpsocket.php';
$Socket = new HTTPSocket;

$Socket->connect('yoursite.com',2222);
$Socket->set_login("{admin_username}","{admin_password}");
// {admin_username} and {admin_password} are not specifically your administrative DA user,
// but rather the user that you want to own this new user.

$Socket->query('/CMD_ACCOUNT_USER',
    array(
		'username' => '', // The User's username. 4-8 characters, alphanumeric
		'email' => '', // A valid email address
		'passwd' => '', // The User's password. 5+ characters, ascii
		'passwd2' => '', // Password double check. 5+ characters, ascii
		'domain' => '', // A valid domain name in the form: domain.com
		'package' => '', // One of the User packages created by the Reseller
		'ip' => '', // One of the ips which is available for user creation. Only free or shared ips are allowed.
		'notify' => 'yes', // yes or no. If yes, an email will be sent to email
		'action' => 'create',
		'add' => 'Submit'
    ));
$result = $Socket->fetch_parsed_body();

if ( $Socket->get_status_code() != 200 || !empty($result['error']) )
{
	// failure.
	echo "Failed to add new user: {$result['error']}<br />{$result['text']}";
}

// new user added.
print_r($result);

?>
 
Last edited:
RosT,

You can do the following:
  1. login through the API as test's owner
  2. grab the array returned by CMD_API_SHOW_USERS
  3. use in_array to test if test exists within the above array[/list=1]
    Phi1.
 
Thanks.
An how to get list of all domains in the directadmin database? Not for one user - for all.
 
I don't see any specific comand for this in the api manual.

So what you can do is get the list of all users, store it as an array. Then have it collect the domains that each owner has and display them. Probably using foreach
 
Last edited:
for exp. I have 2 DA servers
and one database of users (logins, domains and so on)

user on 1 server created mydomain.com

Is it possible by using your php-class disallow user on second server create same domain name? ....DA allow because it is not using same DNS :(
 
If you are adding domains through a custom script that you made and not through Da then it would be possible, just not automatically through the api.

You would have to have your script gather a domain list from both servers and then compare them to the domain trying to be created and if there are any matches fail because of a duplicate.
 
Hello,

It's very good and very easy to install this system. But i've got a little problem, my users register an account, automatically the user is create in directadmin but he is suspended, when the user pay their account with paypal, the account become "unsuspended" automatically, no problem for that. But, i want to send a "welcome message" after unsuspend an account, how I do that ? Thanks. Bye.
 
problem creating a database using API

Hi

I'm struggling to create a database using the API.

I can get a list of existing databases for the user's account, I can create POP mail boxes (this proves my socket and login work correctly) BUT every time I try to create a database i get

Program Error
Details: A segmentation fault has occurred

If I set it up manually with the same name/user/password it works.

If I attempt to create it when one already exists, it tells me it can't because my quota has been reached (i have max 1 db for this account type).

I have tried both /CMD_DB and /CMD_API_DATABASES

Here's the code for my latest attempt:
  • <?php

    include 'httpsocket.php';

    $sock = new HTTPSocket;

    $sock->connect('account ip address',2222);

    $sock->set_login("bloggs","******");

    $sock->set_method('POST');

    $mydata = array(
    'action' => 'create',
    'name' => 'db',
    'user' => 'usr',
    'passwd' => '******');

    $sock->query('/CMD_API_DATABASES',$mydata);

    $result = $sock->fetch_body();

    echo $result;

    ?>

Whats going wrong?
 
Last edited:
Re: problem creating a database using API

mike_p said:
Hi

I'm struggling to create a database using the API.

I can get a list of existing databases for the user's account, I can create POP mail boxes (this proves my socket and login work correctly) BUT every time I try to create a database i get



If I set it up manually with the same name/user/password it works.

If I attempt to create it when one already exists, it tells me it can't because my quota has been reached (i have max 1 db for this account type).

I have tried both /CMD_DB and /CMD_API_POP

Here's the code for my latest attempt:
  • <?php

    include 'httpsocket.php';

    $sock = new HTTPSocket;

    $sock->connect('account ip address',2222);

    $sock->set_login("bloggs","******");

    $sock->set_method('POST');

    $mydata = array(
    'action' => 'create',
    'name' => 'db',
    'user' => 'usr',
    'passwd' => '******',
    'passwd2'=> '******');

    $sock->query('/CMD_API_POP',$mydata);

    $result = $sock->fetch_body();

    echo $result;

    ?>

Whats going wrong?

First I am no expert but I believe you need to use /CMD_API_DATABASES to get this done.

Kind Regards,
Onno Vrijburg
 
Ooops - that was just a typo from my test to create POP boxes instead! [ I will now edit my original post ].

But thanks anyway for a quick response.
 
Last edited:
Can you post the code that gives you the segment fault. From your post I read that when you try to create a database you get a segment fault, is this correct? Or do you get the segment fault when you try to create POP accounts?

Kind Regards,
Onno Vrijburg
 
<?php

include 'httpsocket.inc';

$sock = new HTTPSocket;

$sock->connect('ipaddress',2222);

$sock->set_login("bloggs","bl0ggs");

$sock->set_method('POST');

$mydata = array(
'action' => 'create',
'domain' => 'bloggs.com',
'name' => 'db',
'user' => 'usr',
'passwd' => 'bl0ggs',
'passwd2'=> 'bl0ggs',
'create' => 'Create'
);

$sock->query('/CMD_DB',$mydata);

$result = $sock->fetch_body();

echo $result;

?>


The onlything Ive changed here is the ip address.
Bloggs.com is a dummy account ive set up for testing.
(DNS is handled by my local hosts file)
 
mike_p said:
<?php

include 'httpsocket.inc';

$sock = new HTTPSocket;

$sock->connect('ipaddress',2222);

$sock->set_login("bloggs","bl0ggs");

$sock->set_method('POST');

$mydata = array(
'action' => 'create',
'domain' => 'bloggs.com',
'name' => 'db',
'user' => 'usr',
'passwd' => 'bl0ggs',
'passwd2'=> 'bl0ggs',
'create' => 'Create'
);

$sock->query('/CMD_DB',$mydata);

$result = $sock->fetch_body();

echo $result;

?>


The onlything Ive changed here is the ip address.
Bloggs.com is a dummy account ive set up for testing.
(DNS is handled by my local hosts file)


If you read the API Documentation it says that you should use /CMD_DATABASES or /CMD_API_DATABASES and not /CMD_DB.

Let me know if this works...
 
Thanks but that doesn't seem to give any improvement.

Could someone who has succeeded please post their code?
(Ive ended up trying so many different ways my heads now spinning!)

For the enthusiasts here's my latest attempt using a different account (I don't mind posting account name etc and the DNS is normal)


  • <?php

    include 'httpsocket.inc';

    $sock = new HTTPSocket;

    $sock->connect('is-demo.co.uk',2222);

    $sock->set_login("isdemo","demo99");

    $sock->set_method('POST');

    $mydata = array(
    'action' => 'create',
    // 'domain' => 'is-demo.co.uk',
    'name' => 'db',
    'user' => 'usr',
    'passwd' => 'demo99'
    //'passwd2'=> 'demo99',
    //'create' => 'Create'
    );

    $sock->query('/CMD_API_DATABASES',$mydata);

    $result = $sock->fetch_body();

    echo $result;

    ?>

Even the password is as-is - but will be changed in a few minutes time.

Thanks for any further help.
 
There is also no mention of "domain" or "create" fields in the API Interface for databases.

Regards,
Onno
 
There is also no mention of "domain" or "create" fields in the API Interface for databases.
They're commented out: when uing the /CMD_DB call they are required. By using the CMD_DB I was able to get the list of existing databases.

Thanks for sticking with it!
 
Back
Top