XML API Uploadfiles not working properly?

Visad182

Member
Sep 8, 2017
10
0
1
Canada
cPanel Access Level
Website Owner
Hello everyone.

I'm working on a project right now to create automaticly account and send files after the account creation.

I'm working with XML API.

Right now, createaccs is working very well without any problem.

But i'm trying to make uploadfiles working but i've alway the same error which is

Code:
{"cpanelresult":{"event":{"result":1},"error":"You must specify at least one file to upload.","apiversion":2,"data":[{"uploads":[],"succeeded":0,"failed":0,"warned":0}],"module":"Fileman","func":"uploadfiles"}}
But i already specified the upload file.

Here is my PHP code, and i had tryed everything, every form of path, with / or without...

Still have the same error.

PHP:
<?php
// Must include cPanel API
include "xmlapi.php";
 
// Credentials for cPanel account
$source_server_ip = "xxxx"; // Server IP or domain name eg: 212.122.3.77 or cpanel.domain.tld
$cpanel_account = "xxxx"; // cPanel username
$cpanel_password = "xxxx"; // cPanel password
 
 
$xmlapi = new xmlapi($source_server_ip);
$xmlapi->password_auth($cpanel_account,$cpanel_password);
$xmlapi->set_port('2083');
 

$destination_dir = "home/myAccount/public_html/gros";
$newdir_name = "public_html/wp-links-opml.php";
$api2args = array(
        'dir'=> 'public_html/gros',
        'file-1' => '/home/immobill/accont/wp-links-opml.php'                   
    );     
 
$xmlapi->set_output('json');
print $xmlapi->api2_query($cpanel_account, 'Fileman', 'uploadfiles', $api2args);
 
?>
Can someone tell me what is the problem :\ ?

Will be very appreciated.

Thank
 

Visad182

Member
Sep 8, 2017
10
0
1
Canada
cPanel Access Level
Website Owner
Same thing for this part of your own code in your API doc.


PHP:
<?php
// Must include cPanel API
include "xmlapi.php";
?>
<?php
// Log everything during development.
// If you run this on the CLI, set 'display_errors = On' in php.ini.
error_reporting(E_ALL);

// Declare your username and password for authentication.
$username = 'xxx';
$password = 'xxx';

// Define the API call.
$cpanel_host = 'xxxx';
$request_uri = "https://$cpanel_host:2083/execute/Fileman/upload_files";

// Define the filename and destination.
$upload_file = realpath("/public_html/xxxx/dog/document.jpeg");
$destination_dir = "home/xxxx/public_html";

// Set up the payload to send to the server.
if( function_exists( 'curl_file_create' ) ) {
    $cf = curl_file_create( $upload_file );
} else {
    $cf = "@/".$upload_file;
}
$payload = array(
    'dir'    => $destination_dir,
    'file-1' => $cf
);

// Set up the cURL request object.
$ch = curl_init( $request_uri );
curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt( $ch, CURLOPT_USERPWD, $username . ':' . $password );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );

// Set up a POST request with the payload.
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

// Make the call, and then terminate the cURL caller object.
$curl_response = curl_exec( $ch );
curl_close( $ch );

// Decode and validate output.
$response = json_decode( $curl_response );
if( empty( $response ) ) {
    echo "The cURL call did not return valid JSON:\n";
    die( $response );
} elseif ( !$response->status ) {
    echo "The cURL call returned valid JSON, but reported errors:\n";
    die( $response->errors[0] . "\n" );
}

// Print and exit.
die( print_r( $response ) );
?>



Code:
The cURL call did not return valid JSON:
This is the harder and much complicated API i ever worked with.

Documentation is verry bad explained, all PHP code is not working, have to find it by another github user.

This is crazy.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,270
463
Hello @Visad182,

I recommend against using the XML-like output of our API in your custom script. See the following quote from our March 2017 Development Update Blog:

  • XML-like output of APIs
    The XML-like output of our API (often called the XML API) is already officially deprecated, and cPanel & WHM Version 70 will be the last version to support this output. Integrators and API users will want to begin switching to use the JSON output of the API now. The XML-like format has long caused problems for integrators and developers because it is not valid XML, and we find that removing it completely will help reduce that.
Here's the full PHP example from our Tutorial - Use UAPI's Fileman::upload_files Function in Custom Code document:

Code:
<?php
// Log everything during development.
// If you run this on the CLI, set 'display_errors = On' in php.ini.
error_reporting(E_ALL);

// Declare your username and password for authentication.
$username = 'example';
$password = 'luggage12345';

// Define the API call.
$cpanel_host = 'localhost';
$request_uri = "https://$cpanel_host:2083/execute/Fileman/upload_files";

// Define the filename and destination.
$upload_file = realpath("/path/to/fileto.upload");
$destination_dir = "public_html";

// Set up the payload to send to the server.
if( function_exists( 'curl_file_create' ) ) {
    $cf = curl_file_create( $upload_file );
} else {
    $cf = "@/".$upload_file;
}
$payload = array(
    'dir'    => $destination_dir,
    'file-1' => $cf
);

// Set up the cURL request object.
$ch = curl_init( $request_uri );
curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt( $ch, CURLOPT_USERPWD, $username . ':' . $password );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );

// Set up a POST request with the payload.
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

// Make the call, and then terminate the cURL caller object.
$curl_response = curl_exec( $ch );
curl_close( $ch );

// Decode and validate output.
$response = json_decode( $curl_response );
if( empty( $response ) ) {
    echo "The cURL call did not return valid JSON:\n";
    die( $response );
} elseif ( !$response->status ) {
    echo "The cURL call returned valid JSON, but reported errors:\n";
    die( $response->errors[0] . "\n" );
}

// Print and exit.
die( print_r( $response ) );
?>
Additionally, can you clarify how you will be uploading the files in this project? For instance, is it the same files every time, or are you manually uploading custom files during each account creation in your project?

Thank you.
 

Visad182

Member
Sep 8, 2017
10
0
1
Canada
cPanel Access Level
Website Owner
Hello Michael, thank for your help.

Finaly, files was uploaded.

I think problem was files paths...

Here is the working code:

PHP:
<?php
// Log everything during development.
// If you run this on the CLI, set 'display_errors = On' in php.ini.
error_reporting(E_ALL);

// Declare your username and password for authentication.
$username = 'xxxxx';
$password = 'xxxxxxx"
// Define the API call.
$cpanel_host = 'localhost';
$request_uri = "https://$cpanel_host:2083/execute/Fileman/upload_files";

// Define the filename and destination.
$upload_file = realpath("/home/trycp/try.zip");
$destination_dir = "public_html/try";

// Set up the payload to send to the server.
if( function_exists( 'curl_file_create' ) ) {
    $cf = curl_file_create( $upload_file );
} else {
    $cf = "@/".$upload_file;
}
$payload = array(
    'dir'    => $destination_dir,
    'file-1' => $cf
);

// Set up the cURL request object.
$ch = curl_init( $request_uri );
curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt( $ch, CURLOPT_USERPWD, $username . ':' . $password );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );

// Set up a POST request with the payload.
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

// Make the call, and then terminate the cURL caller object.
$curl_response = curl_exec( $ch );
curl_close( $ch );

// Decode and validate output.
$response = json_decode( $curl_response );
if( empty( $response ) ) {
    echo "The cURL call did not return valid JSON:\n";
    die( $response );
} elseif ( !$response->status ) {
    echo "The cURL call returned valid JSON, but reported errors:\n";
    die( $response->errors[0] . "\n" );
}

// Print and exit.
die( print_r( $response ) );
?>
Just a little question @cPanelMichael about this path
/home/trycp/try.zip

Does this zip in this path (trycp is the username of cpanel user), so can you confirm me this ZIP files right at this path can't be accessible from other person except the respective Cpanel user ?

I just want this ZIP to not be accessible to anybody except via the API, with cpanel username and everything... so, this is a protected path right ?

thank
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,270
463
Just a little question @cPanelMichael about this path
/home/trycp/try.zip

Does this zip in this path (trycp is the username of cpanel user), so can you confirm me this ZIP files right at this path can't be accessible from other person except the respective Cpanel user ?

I just want this ZIP to not be accessible to anybody except via the API, with cpanel username and everything... so, this is a protected path right ?
Hello,

That path is not accessible to the public because it's outside of the default document root of your website. Accessing that path requires authentication with the cPanel username and password.

Thank you.