Need help with remote access to cPanel API for creating secondary FTP accounts

daniott

Registered
Mar 2, 2011
2
0
51
Hello,

I've spent hours searching through the cPanel API docs, forums, etc. trying to find a solution to what I think is a fairly simple problem. I'm new to cPanel and fairly new to Php although I have a fair amount of programming experience.

I am trying to figure out how to access cPanel remotely from a Php script so I can add secondary FTP accounts to my web hosting account.

The client management software I am running on my domain provides a module that runs in the background written in Php that I can modify to perform remote tasks and then return status. (Note that this module executes in the background after a new customer successfully signs up - so there is no browser involved for input or output from/to the customer).

It would really help if somebody could show me an example of how this could be done with the XMLAPI. Since that seems easiest (but any method that works is fine).

Any help is appreciated!
Dan
 

cPanelDavidN

Well-Known Member
Staff member
Dec 17, 2009
571
3
68
Houston, TX
cPanel Access Level
Root Administrator
Hi Dan,

You should take a look at our XML-API PHP client class. Its a PHP class that lets you make remote calls to a cPanel system. It's really ease to code with. Pretty much all you have to do is know your credential and the details of the API call you want. There are a few examples in the tarball; below is an example of how to call API2 Ftp::addftp (used to add FTP accounts)

PHP:
include("xmlapi.php");

$ip = "10.1.1.1";  //your server IP or name

$xmlapi = new xmlapi($ip);  // instantiate the XML-API object

//// If you need root or WHM/reseller level access, set root credentials and use port 2087/2086 
//$auth_user = "root";
//$auth_pass = "rootS3creT!";
//$xmlapi->set_port(2087);

//// If you're just authentiating against you cPanel, set cPanel credentials and use port 2083/2082
/// for this example, I'll be "dave"
$auth_user = "dave";
$auth_pass = "daveS3creT!";
$xmlapi->set_port(2083);

//store our auth in object
$xmlapi->password_auth($auth_user, $auth_pass);

//// Optional stuff
// JSON or XML output
$xmlapi->set_output('xml');
// Debugging, great for at the console
$xmlapi->set_debug(1);

//// From API2 documents, this is want input is required by 'addftp' function
/// Ftp::addftp( user, pass, homedir, quota, disallowdot, homedir2 )

$args = array( 
  'user'=>'[email protected]',
  'pass'=>'frc!%2Z2ltD',
  'quota'=>20
);

// for any cPanel API1 or API2 query you'll need to say what cPanel account you are affecting
$cpuser = 'dave';

$response = $xmlapi->api2_query($cpuser, 'Ftp', 'addftp', $args);
Checkout the links in my signature and search the forums for PHP XMLAPI and I'm sure you'll find tons of useful info ;)

Let me know if you have further questions,
-DavidN
 

daniott

Registered
Mar 2, 2011
2
0
51
David,

Thanks so much for your help on this - what you posted was exactly what I needed and it enabled me to get this thing working!

It turned out however that I was not able to use the API2_query function as mentioned in your example and had to use API1_query instead because my cPanel version (11.26.20) did not support the API2 addftp function. (Only supported in 11.27 and after).

That meant I had to figure out that API1 in my cPanel also supports an addftp function and that in order to access it I needed to call API1_query instead of API2_query. From there it was a matter of understanding the difference in how the argument array is processed in API1_qurey versus API2_qurey.

All in all a very instructive problem for me. Thanks again to you for your help and guidance.
Dan
 

cPanelDavidN

Well-Known Member
Staff member
Dec 17, 2009
571
3
68
Houston, TX
cPanel Access Level
Root Administrator
Glad I could help. You bring up a great point about the availability of API2 addftp only in 11.27+.

But it sounds like your well on your way! and yes API1 takes ordered parameters (and ordinal array) as opposed to API2 which takes named parameters (an associative array). The order for API1 Ftp::addftp should be:
PHP:
$args = array( $user, $pass, $homedir, $quota, $disallowdot, $homedir2 );
Best Regards,
-DavidN