Unable to use httpsocket for API

Jordz2203

Verified User
Joined
Sep 8, 2022
Messages
75
echo 'running<br>';
include 'httpsocket.php';

$dbuser = "directadminusername_dbusername";
$dbname = "directadminusername_dbname";
$dbpass = "directadminusername_dbpassword";

$sock = new HTTPSocket;
$sock->connect('za1.ccstudio.co.za',2222);
// If I remove https:// from the above link I get the following message from the echo at the bottom "use https"
// If I add https:// then no echo from below, only the above echo of "running"

$sock->set_login("adminusername|clientusername","workingadminpassword");

$sock->set_method('POST');

$sock->query('/CMD_API_DATABASES',
array(
'action' => 'create',
'name' => $dbname,
'user' => $dbuser,
'passwd' => $dbpass,
'passwd2' => $dbpass
));

$result = $sock->fetch_body();
echo $result;

?>[/CODE]

For some reason the above doesn't work, I have this code on the httpsocket.php

The error is describe in the comments under the $sock->connect


EDIT: Just realized how old the reference post on this forum is https://forum.directadmin.com/threads/create-database-api.41397/
 
Last edited:
Check directadmin logs for possible errors under /var/log/directadmin/
Error log is empty, in activity log I can see this but this is the only thing that sticks out
2022-11-13T14:14:52+02:00 0.004 77.75.122.198 POST /CMD_API_DATABASES - - - -
 
trying use something like this
commend line your
//$sock->connect
and just use like this
Code:
$sock->query('https://your.server.com:2222/CMD_API_DATABASES');
 
These should be corrected

PHP:
$dbuser = "dbuser";
$dbname = "dbname";
$dbpass = "directadminusername_dbpassword";

When running for user "clientusername" it will creates names:

DB user: clientusername_dbname
DB name: clientusername_dbname

But these errors don't explain the output of your API call.

Just tested your code on my side:

PHP:
<?php
echo 'running<br>';
include 'httpsocket.php';

$dbuser = "dbtest2";
$dbname = "dbtest2";
$dbpass = "dbP@ssw0rd";

$sock = new HTTPSocket;
$sock->connect('ssl://server.example.net',2222);
$sock->set_login("myadmin|client","adminpassword");
$sock->set_method('POST');
$sock->query('/CMD_API_DATABASES?json=yes',
    array(
        'action' => 'create',
        'name' => $dbname,
        'user' => $dbuser,
        'passwd' => $dbpass,
        'passwd2' => $dbpass
    ));

$result = $sock->fetch_body();
echo $result;

and it's working fine, and I got the following output:

Code:
running<br>{
        "result": "Database Created",
        "success": "Database Created"
}

So you need to check your setup and logs for more clues.
 
These should be corrected

PHP:
$dbuser = "dbuser";
$dbname = "dbname";
$dbpass = "directadminusername_dbpassword";

When running for user "clientusername" it will creates names:

DB user: clientusername_dbname
DB name: clientusername_dbname

But these errors don't explain the output of your API call.

Just tested your code on my side:

PHP:
<?php
echo 'running<br>';
include 'httpsocket.php';

$dbuser = "dbtest2";
$dbname = "dbtest2";
$dbpass = "dbP@ssw0rd";

$sock = new HTTPSocket;
$sock->connect('ssl://server.example.net',2222);
$sock->set_login("myadmin|client","adminpassword");
$sock->set_method('POST');
$sock->query('/CMD_API_DATABASES?json=yes',
    array(
        'action' => 'create',
        'name' => $dbname,
        'user' => $dbuser,
        'passwd' => $dbpass,
        'passwd2' => $dbpass
    ));

$result = $sock->fetch_body();
echo $result;

and it's working fine, and I got the following output:

Code:
running<br>{
        "result": "Database Created",
        "success": "Database Created"
}

So you need to check your setup and logs for more clues.
I added ?json=yes and it works now :) thank you! @zEitEr
 
These should be corrected

PHP:
$dbuser = "dbuser";
$dbname = "dbname";
$dbpass = "directadminusername_dbpassword";

When running for user "clientusername" it will creates names:

DB user: clientusername_dbname
DB name: clientusername_dbname

But these errors don't explain the output of your API call.

Just tested your code on my side:

PHP:
<?php
echo 'running<br>';
include 'httpsocket.php';

$dbuser = "dbtest2";
$dbname = "dbtest2";
$dbpass = "dbP@ssw0rd";

$sock = new HTTPSocket;
$sock->connect('ssl://server.example.net',2222);
$sock->set_login("myadmin|client","adminpassword");
$sock->set_method('POST');
$sock->query('/CMD_API_DATABASES?json=yes',
    array(
        'action' => 'create',
        'name' => $dbname,
        'user' => $dbuser,
        'passwd' => $dbpass,
        'passwd2' => $dbpass
    ));

$result = $sock->fetch_body();
echo $result;

and it's working fine, and I got the following output:

Code:
running<br>{
        "result": "Database Created",
        "success": "Database Created"
}

So you need to check your setup and logs for more clues.
Is it possible to do SSH commands via the API?
 
Back
Top