Decode GZIP response in Laravel
1 min read

Decode GZIP response in Laravel

Decoding GZIP response in Laravel using Guzzle or the HTTP client

If you try to decode a GZIP response without decoding, you will have seen this error

XMLReader::read(): /var/www/html/:1: parser error : Document is empty {"exception":"[object] (ErrorException(code: 0): XMLReader::read(): /var/www/html/:1: parser error : Document is empty at

To decode a XML ZIP response in Guzzle, you can do this as recommended by the documentation

// Request gzipped data, but do not decode it while downloading
$client->request('GET', 'foo.xml.gz', [
    'headers'        => ['Accept-Encoding' => 'gzip'],
    'decode_content' => false
]);

When set to a string, the bytes of a response are decoded and the string value provided to the decode_content option is passed as the Accept-Encoding header of the request.

// Pass "gzip" as the Accept-Encoding header.
$client->request('GET', 'foo.xml.gz', ['decode_content' => 'gzip']);

Laravel provides useful header methods via its HTTP client.

$response = Http::withOptions([
            'decode_content' => 'gzip'
        ])->get('foo.xml.gz');

or using headers, this can be

   $response = Http::accept('gzip')
				->get('foo.xml.gz');