Edit Documents using REST API in PHP

Edit Documents using REST API in PHP.

We can add, edit or delete the content of existing Word documents, Excel spreadsheets, or text files programmatically on the cloud. We can also apply text formatting in the documents using PHP without installing any external application. In this article, we will learn how to edit documents using a REST API in PHP.

The following topics shall be covered in this article:

Document Editor REST API and PHP SDK

We will be using the PHP SDK of GroupDocs.Editor Cloud API for modifying the DOCXXLSX, and TXT files. It allows editing documents of the supported formats. Please install it using the following command in the console:

composer require groupdocscloud/groupdocs-editor-cloud

After installation, please use the Composers’ autoload to use the SDK as shown below:

require_once('vendor/autoload.php');

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Edit Word Documents using REST API in PHP

We can edit Word documents by following the simple steps mentioned below:

  1. Upload the DOCX file to the cloud
  2. Edit the uploaded Word document
  3. Download the edited file

Upload the Document

Firstly, we will upload the DOCX file to the cloud using the code sample given below:

As a result, the uploaded DOCX file will be available in the files section of the dashboard on the cloud.

Edit Word Document in PHP

Now, we will edit the content of the uploaded DOCX file by following the steps given below:

  • Firstly, create instances of the FileApi and the EditApi.
  • Next, provide the uploaded DOCX file path.
  • Then, download the file as an HTML document.
  • Next, read the downloaded HTML file as a string.
  • Then, edit the HTML and save the updated HTML document.
  • After that, upload the updated HTML file.
  • Finally, save HTML back to DOCX using the EditApi.save() method.

The following code sample shows how to edit a Word document using a REST API in PHP.

Edit Word Documents using REST API in PHP.
Edit Word Document using a REST API in PHP.

Download the Updated File

The above code sample will save the edited Word file (DOCX) on the cloud. It can be downloaded using the following code sample:

Modify Excel Spreadsheets using REST API in PHP

We can edit the content of an Excel sheet by following the steps mentioned earlier. However, we just need to provide the uploaded XLSX file path.

The following code sample shows how to edit Excel sheet data using a REST API in PHP.

Modify Excel Spreadsheets using REST API in PHP.
Modify Excel Spreadsheet using a REST API in PHP.

Update Text Files using REST API in PHP

We can also update the content of a text file by following the steps mentioned earlier. However, we just need to provide the uploaded TXT file path.

The following code sample shows how to edit a text file using a REST API in PHP.

Update Text Files using REST API in PHP.
Update Text File using a REST API in PHP.

Try Online

Please try the following free online document editing tools, which are developed using the above API.

Conclusion

In this article, we have learned how to:

  • edit or modify the content of Word, Excel, or Text files in PHP;
  • upload DOCX file to the cloud;
  • download updated Word file from the cloud.

Besides, you can learn more about GroupDocs.Editor Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also