How to Zip Files with PHP

Learn how to use pdfRest Zip Files API Tool with PHP to zip processed files for easy downloads.
Share this page

Why Use Zip Files with PHP?

The pdfRest Zip Files API Tool provides a convenient way to programmatically zip multiple files together. This API is particularly useful when dealing with a collection of documents that need to be compressed into a single archive for easier distribution or storage. By using this API, developers can automate the process of zipping files directly from their PHP applications, saving time and reducing the potential for human error.

In a real-world scenario, a user might need to zip a collection of financial reports, project documents, or a batch of images to send as a single attachment via email. The pdfRest Zip Files API allows users to do this directly from their web application, without the need for manual intervention or desktop zip utilities. This streamlines workflows, especially when dealing with large numbers of files or when this process needs to be repeated regularly.

Zip Files with PHP Code Example

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

$client = new Client();
$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];
$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file1', 'r'),
      'filename' => '/path/to/file1',
      'headers'  => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file2', 'r'),
      'filename' => '/path/to/file2',
      'headers'  => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'id[]',
      'contents' => 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_zip'
    ]
]];

$request = new Request('POST', 'https://api.pdfrest.com/zip', $headers);

$res = $client->sendAsync($request, $options)->wait();

echo $res->getBody();

Reference to the provided code: GitHub Repository.

Breaking Down the Code

The code provided is a PHP script that uses the Guzzle HTTP client to send a POST request to the pdfRest API to zip files together. The script includes the following components:

  • GuzzleHttp\Client: Initializes the HTTP client.
  • Headers: Sets the 'Api-Key' required for authentication with the pdfRest API.
  • Options: Defines the multipart payload, including the files to be zipped and additional parameters like 'output' to specify the format of the response.
  • Request: Creates a new HTTP POST request with the API endpoint and headers.
  • Response: Sends the asynchronous request and waits for the response, then outputs the response body.

Each 'multipart' array element represents a part of the request payload:

  • The 'name' and 'contents' keys are used to specify the files to be included in the zip.
  • The 'filename' key is used to provide the path to the file.
  • The 'headers' key within each file part allows you to set the 'Content-Type' header for the file being uploaded.
  • The 'id[]' key is used to include the ID of a file already stored on the pdfRest server.
  • The 'output' key specifies the output format, in this case, 'pdfrest_zip' to indicate a zip file.

According to the pdfRest Cloud API Reference Guide, each parameter in the request is essential for the API to understand what files to include and how to format the response.

Beyond the Tutorial

This tutorial has demonstrated how to use PHP to call the pdfRest Zip Files API to compress multiple files into a single zip archive. By following the provided code example and understanding each part of the code, you can integrate this functionality into your PHP applications.

To further explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information on other endpoints and parameters, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads for the pdfRest API can be found at GitHub Repository.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us