We're sorry AsposeCloud doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.cloud

Append Multiple Word Documents in PHP with Aspose.Words REST API Throws Error 400

The following is written with the official aspose.cloud PHP SDK. Can some one explain to me what seems to be the problem with this. I cant figure out how the service uses paths. for example the /progen/ folder is a folder in my website`s root. files exists in subdirectories of that directory. the /progen/ is also the cwd. without specifying it I still get the same error.

                    $files_docs = array();
                    for($i = 1; $i < count($files); $i++){
                            $f = "/progen/".$files[$i];
                             $entry = new DocumentEntry(array("href" => $f, "import_format_mode" => "KeepSourceFormatting"));
                            array_push($files_docs, $entry);
                    }
                    $docList = new DocumentEntryList();
                    $docList->setDocumentEntries($files_docs);
                    
                    $f = "/progen/".$files[0];
                    $request = new Requests\AppendDocumentRequest($f, $docList, null, null, null, null, null, $fname);
                    echo json_encode($this->wordsApi->appendDocument($request));

The result i get is:

Type: Aspose\Words\ApiException

Message: [400] Error connecting to the API (https://api.aspose.cloud/v4.0/words/tmp/progen/prop/1646213908/cvs/BAHA’%20ALSHALALFAH%2C%20PhD.docx/appendDocument?destFileName=progen%2Ftmp%2Fprop%2F1646213908%2Fcvs.docx)

Thank you for your support

@sherifeweis

I append multiple word documents in PHP using Aspose.Words REST API without any problem. Please note appendDocument API method appends document from cloud storage and saves the output to cloud storage. I am unable to test your code due to incomplete code. However, please check the following working code. It uploads files from a folder in the current working directory to cloud storage, append the document and download the output file. Hopefully, it will help you to accomplish the task.

Steps to Merge Word Files in PHP

  1. Get aspose.cloud credentials
  2. Install Aspose.Words Cloud SDK for PHP package from PIP
  3. Create a script file for PHP code and add reference to Aspose.Words Cloud SDK for PHP
  4. Initiate WordsApi object with credentials
  5. Upload source Word files to cloud storage using UploadFile API method
  6. Create requestDocumentListDocumentEntries Array to merge word files
  7. Use AppenDocumentRequest method to set desired properties
  8. Merge Word files with appendDocument method and save output to cloud storage

Combine Word Files in PHP

<?php

require_once('D:\xampp\htdocs\aspose-words-cloud-php-master\vendor\autoload.php');

//TODO: Get your ClientId and ClientSecret at https://dashboard.aspose.cloud (free registration is required).
$ClientId="xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx";
$ClientSecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxx";


$wordsApi = new Aspose\Words\WordsApi($ClientId,$ClientSecret);


try {

	$remoteDataFolder = "Temp";
        $localFile = "data/02_pages.docx";
        $remoteFileName = "02_pages.docx";
	$localFile1 = "data/Test.docx";
        $remoteFileName1 = "Test.docx";
        $outputFileName = "TestAppendDocument.docx";

        
	$uploadRequest = new Aspose\Words\Model\Requests\UploadFileRequest($localFile,$remoteDataFolder."/".$remoteFileName,null);
	$wordsApi->uploadFile($uploadRequest);
	$uploadRequest1 = new Aspose\Words\Model\Requests\UploadFileRequest($localFile1,$remoteDataFolder."/".$remoteFileName1,null);
	$wordsApi->uploadFile($uploadRequest1);

        $requestDocumentListDocumentEntries0 = new Aspose\Words\Model\DocumentEntry(array(
            "href" => $remoteDataFolder . "/" . $remoteFileName1,
            "import_format_mode" => "KeepSourceFormatting",
        ));
        $requestDocumentListDocumentEntries = [
            $requestDocumentListDocumentEntries0,
        ];
        $requestDocumentList = new Aspose\Words\Model\DocumentEntryList(array(
            "document_entries" => $requestDocumentListDocumentEntries,
        ));
        $request = new Aspose\Words\Model\Requests\AppendDocumentRequest(
            $remoteFileName,
            $requestDocumentList,
            $remoteDataFolder,
            NULL,
            NULL,
            NULL,
            $remoteDataFolder . "/" . $outputFileName,
            NULL,
            NULL
        );

        $result = $wordsApi->appendDocument($request);
		
	##Download file 
	$request = new Aspose\Words\Model\Requests\DownloadFileRequest($remoteDataFolder."/".$outputFileName,NULL,NULL);
	$result = $wordsApi->downloadFile($request);
	copy($result->getPathName(),"data/AppendOutput.docx");
	print_r($result);
		
		
    
} catch (Exception $e) {
    echo  "Something went wrong: ",  $e->getMessage(), "\n";
    PHP_EOL;
}

?>