Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

XML API Uploadfiles not working properly?

Discussion in 'cPanel Developers' started by Visad182, Jul 19, 2018.

Tags:
  1. Visad182

    Visad182 Member

    Joined:
    Sep 8, 2017
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    fdggfd
    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
     
  2. Visad182

    Visad182 Member

    Joined:
    Sep 8, 2017
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    fdggfd
    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$chCURLOPT_HTTPAUTHCURLAUTH_BASIC );
    curl_setopt$chCURLOPT_USERPWD$username ':' $password );
    curl_setopt$chCURLOPT_SSL_VERIFYHOSTfalse );
    curl_setopt$chCURLOPT_SSL_VERIFYPEERfalse );

    // Set up a POST request with the payload.
    curl_setopt$chCURLOPT_POSTtrue );
    curl_setopt$chCURLOPT_POSTFIELDS$payload );
    curl_setopt$chCURLOPT_RETURNTRANSFERtrue );

    // 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.
     
  3. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    44,803
    Likes Received:
    1,898
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    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:

    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Visad182

    Visad182 Member

    Joined:
    Sep 8, 2017
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    fdggfd
    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
     
  5. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    44,803
    Likes Received:
    1,898
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice