Free Support Forum -

Perform Microsoft Word Mail Merge in PHP using Aspose.Words REST API

I’m getting started on Aspose Cloud for Word in PHP. At the outset the experience so far has been highly frustrating. There is very few examples and no complete examples where I can see say an upload, merge, convert and download. My time has been spent looking at the composer code and trying to understand the results without any documentation.

Issue 1:
Calling getListFiles returns no results, I get a {} with an empty path (or I try directory) with or without the storage name. Nothing.

Issue 2:
Calling download file returns no results.

Issue 3:
Calling postDocumentExecuteMailMerge seems to work. I get an object back but I’m not able to do anything with [document][links][15][href]? I can an authentication denied in the browser when attempting the URL and I can’t see any methods on your SDK to download it?

Issue 4:
I’m unsure if I need Aspose PDF installed to also access PDFs after mail merge.

I would appreciate a prompt response. I’ve tried to be fairly patient but I’m not getting anywhere.



We are sorry for your inconvenience. We are in process of updating the examples of different platforms in the documentation. However, meanwhile you may please check complete unit test of APIs for the purpose.

I am creating a sample code for your other issues and will share it soon.

Thanks Tilal,

A quick update. Issue 2 is no longer a problem, an ajax response was sanitizing the object data in my testing.


Thanks for your feedback.

The getListFiles is working fine at my end. Please find sample code below.

The postDocumentExecuteMailMerge saves output file to storage and returns possible options to convert resultant document to other supported formats. You can convert supported documents to other formats using GetDocumetnWithFormat API. You can also get the sample PHP unit test for it from github repo.

Yes, Please note if you want to manipulate the PDF file then you can use Aspose.Pdf Cloud API.



//TODO: Get your AppSID and AppKey at (free registration is required).


$configuration = new Aspose\Words\Configuration();

$wordsApi = new Aspose\Words\WordsApi(null,$configuration); 

$storageApi = new Aspose\Storage\Api\StorageApi();

try {
	## List the files and folders
	$path = "output";
    $storage = null;
    $request = new Aspose\Storage\Model\Requests\GetListFilesRequest($path, $storage);
    $result = $storageApi->getListFiles($request);
	$templateName = "SampleMailMergeTemplate.docx";
    $outputName = "ExecuteMailMerge_output.docx";
	##read mailmerge data
    $data = file_get_contents("SampleMailMergeTemplateData.txt");
	##upload template file to storage
    $putRequest = new Aspose\Storage\Model\Requests\PutCreateRequest($templateName, $templateName);
	##Execute MailMerge
    $request = new Aspose\Words\Model\Requests\PostDocumentExecuteMailMergeRequest($templateName, $data, null,null, null, null, null,null, null, false, $outputName);
    $result = $wordsApi->postDocumentExecuteMailMerge($request);
	## Download a file 		
	$path = "ExecuteMailMerge_output.docx";
	$versionId = null;
    $storage = null;
    $request = new Aspose\Storage\Model\Requests\GetDownloadRequest($path, $versionId, $storage);
    $result = $storageApi->getDownload($request);	
    echo "Status: ",  $result->getSize();		

} catch (Exception $e) {
    echo  "Something went wrong: ",  $e->getMessage(), "\n";