How to Unzip Files with PHP
Why Unzip Files with PHP?
The pdfRest Zip Files API Tool provides a powerful and efficient way to manage zip files through API calls. By leveraging this tool, developers can easily integrate unzip file functionalities into their PHP applications. This tutorial will guide you through the process of sending an API call to the pdfRest service to unzip files using PHP, offering a seamless and automated solution for extracting and managing compressed files.
Users often need to handle large volumes of data, such as images, documents, or backups. Unzipping these files can facilitate efficient processing and access to individual files. For instance, a company might use the Zip Files API to extract daily backup files received in a compressed format, ensuring quick access and efficient data management.
Unzip Files with PHP Code Example
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client. use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace. use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class. use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams. $client = new Client(); // Create a new instance of the Guzzle HTTP client. $headers = [ 'Api-Key' =--> 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication. ]; $options = [ 'multipart' => [ [ 'name' => 'file', // Specify the field name for the file. 'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading. 'filename' => 'file.zip', // Set the filename for the file to be processed, in this case, 'file.zip'. 'headers' => [ 'Content-Type' => '' // Set the Content-Type header for the file. ] ] ] ]; $request = new Request('POST', 'https://api.pdfrest.com/unzip', $headers); // Create a new HTTP POST request with the API endpoint and headers. $res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response. echo $res->getBody(); // Output the response body, which contains the unzipped content.
Source: GitHub
Breaking Down the Code
The code begins by importing necessary classes from the Guzzle HTTP client library, which is used to send HTTP requests in PHP. The require 'vendor/autoload.php';
line ensures that all necessary classes are loaded.
use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Utils;
Next, a new instance of the Guzzle HTTP client is created with $client = new Client();
. This client will be used to send the API request.
The $headers
array is defined to include the 'Api-Key' header, which is crucial for authenticating the API request. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.
$headers = [ 'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ];
The $options
array specifies the multipart form data for the request. The 'name' field is set to 'file', indicating the file parameter. The 'contents' field uses Utils::tryFopen('/path/to/file', 'r')
to open the specified file for reading. The 'filename' field sets the name of the file being uploaded, and the 'headers' array includes the 'Content-Type' header.
$options = [ 'multipart' => [ [ 'name' => 'file', 'contents' => Utils::tryFopen('/path/to/file', 'r'), 'filename' => 'file.zip', 'headers' => [ 'Content-Type' => '' ] ] ] ];
A new POST request is created with the API endpoint 'https://api.pdfrest.com/unzip'
and the headers using new Request('POST', 'https://api.pdfrest.com/unzip', $headers);
.
$request = new Request('POST', 'https://api.pdfrest.com/unzip', $headers);
The asynchronous request is sent using $client->sendAsync($request, $options)->wait();
, and the response body is outputted with echo $res->getBody();
.
$res = $client->sendAsync($request, $options)->wait(); echo $res->getBody();
Beyond the Tutorial
This tutorial demonstrated how to use PHP to send an API call to the pdfRest service to unzip files. By following these steps, you can integrate similar functionalities into your applications, enhancing your file management capabilities.
To explore more features and tools offered by pdfRest, visit the API Lab. For detailed documentation, refer to the API Reference Guide.
Note: This example showcases a multipart API call. For examples using JSON payloads, visit this GitHub repository.