php-extended / php-api-endpoint-http-gzip
An implementation of the php-extended/php-api-endpoint-http-interface that unzip data on the fly when the responses are queried
Requires
- php: >=7.4
- ext-zip: *
- php-extended/php-api-endpoint-http-interface: ^6.1
- php-extended/polyfill-php80-stringable: ^1.2
Requires (Dev)
This package is auto-updated.
Last update: 2023-08-15 15:50:19 UTC
README
An implementation of the php-extended/php-api-endpoint-http-interface that unzip data on the fly when the responses are queried.
Installation
The installation of this library is made via composer and the autoloading of all classes of this library is made through their autoloader.
- Download
composer.phar
from their website. - Then run the following command to install this library as dependency :
php composer.phar php-extended/php-api-endpoint-http-gzip ^6
Basic Usage
This library needs multiple conditions for it to work :
- First, The request must contain an
X-Php-Download-File
header pointing to a possible valid file in an existing and writeable directory. This is where the downloaded file will be downloaded by the http client. - Second, an http client that supports the
X-Php-Download-File
header to write the body of the http request in that file. - Third, the http client must set an
X-Request-Header-X-Php-Download-File
header in the response that points to that same file. If it does not, it may set anX-Php-Download-File
header instead. - Fourth, the http client must give a Response with a stream that points to this file.
What this endpoint does is, for each time a file is requested, it will try to
uncompress it in the same directory as it is, with a new name. It will replace
the existing X-Request-Header-X-Php-Download-File
and
X-Php-Download-File
headers in the request to point to the new uncompressed
file, and will change the stream of the response with a new stream pointing to
the new file. The old file that was requested at the X-Php-Download-File
header in the request is deleted if the uncompress operation is successful.
/* @var $httpEndpoint \PhpExtended\Endpoint\EndpointInterface */
$gzipEndpoint = new GzipHttpEndpoint($httpEndpoint);
$stream = $gzipEndpoint->httpGet($gzipEndpoint->createUri('https://example.com/path/to/file.gz'), [
'X-Php-Download-File' => '/tmp/file.gz',
]);
/* @var $stream \Psr\Http\Message\StreamInterface */
Once you're finished with the stream you can delete the file (using the metadata
of the stream you can find the file name).
Alternatively, using $gzipEndpoint->httpGetResponse()
you will get
the http response instead of only the stream and can get the file path with
the X-Php-Download-File
header.
License
MIT (See license file).