UAPI PHP creating a ftp account ... There was a problem fetching the env variablecontaining the path to the socket

Mr.Newlove

Registered
Jan 8, 2020
4
1
3
Canada
cPanel Access Level
Root Administrator
Hello all,

I am very new using the cpanel/whm APIs and I am having an issue just getting the library to load.

Here is the basic code I am trying:

Code:
<?php
include("/usr/local/cpanel/php/cpanel.php");

$cpanel = new CPANEL(); // Connect to cPanel - only do this once.

// Create a new FTP user.
$new_ftp = $cpanel->uapi(
    'Ftp', 'add_ftp',
    array(
        'user'    => '[email protected]',
        'pass'    => '1234567890qwertyuiopxx',
        'quota'   => '100',
    )
);
But it only returns an error 500 and this is the error log:

Code:
[08-Jan-2020 09:29:41 UTC] PHP Fatal error:  Uncaught Error: Class 'CPANEL' not found in /home/xxxxxxxxxx/ftp_account_test.php:4
Stack trace:
#0 {main}
  thrown in /home/xxxxxxxxxx/ftp_account_test.php on line 4
[08-Jan-2020 09:36:48 UTC] PHP Fatal error:  Uncaught RuntimeException: There was a problem fetching the env variablecontaining the path to the socket in /usr/local/cpanel/php/cpanel.php:146
Stack trace:
#0 /home/xxxxxxxxxx/ftp_account_test.php(4): CPANEL->__construct()
#1 {main}
  thrown in /usr/local/cpanel/php/cpanel.php on line 146
I would really like to use the built-in PHP class rather than rely on curls to the cpanel session URL stuff as this is way cleaner for what I will end up needing to do for managing FTP accounts automatically.

Thanks,
Kyle


Edit: As of right now, this is the only solution I've found even though its not ideal: N1ghteyes/cpanel-UAPI-php-class
 
Last edited:

cPanelLauren

Product Owner II
Staff member
Nov 14, 2017
13,266
1,300
363
Houston

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
97
50
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
Hey @Mr.Newlove ,

The LiveAPI system will only run locally on the server. In order to call the API remotely, you'll need an API Token for authentication that should be contained in the header of an HTTP request. If you want to make the request as root masquerading a cPanel user, use a WHM API Token. If you want to make the request as a specific cPanel user, use a cPanel API Token.

An example of an HTTP request and header using an API token can be found here.
 
  • Like
Reactions: cPanelLauren