Skip to content

Commit 28a5239

Browse files
committed
:octocat: +Psr7\decompress_content()
1 parent fcda500 commit 28a5239

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ These static methods can be found in the `chillerlan\HTTP\Psr7` namespace:
9999
- `get_json(ResponseInterface $response, bool $assoc = null)`
100100
- `get_xml(ResponseInterface $response)`
101101
- `message_to_string(MessageInterface $message)` - returns the string representation of a `MessageInterface`
102+
- `decompress_content(MessageInterface $message)` - decompresses the message content according to the `Content-Encoding` header and returns the decompressed data
102103

103104
### [PSR-17](https://www.php-fig.org/psr/psr-17/) Factory helpers
104105
These static methods can be found in the `chillerlan\HTTP\Psr17` namespace:

src/Psr7/message_helpers.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,23 @@ function message_to_string(MessageInterface $message):string{
434434

435435
return $msg."\r\n\r\n".$message->getBody();
436436
}
437+
438+
/**
439+
* Decompresses the message content according to the Content-Encoding header and returns the decompressed data
440+
*
441+
* @param \Psr\Http\Message\MessageInterface $message
442+
*
443+
* @return string
444+
*/
445+
function decompress_content(MessageInterface $message):string{
446+
$data = $message->getBody()->getContents();
447+
448+
switch($message->getHeaderLine('content-encoding')){
449+
# case 'br' : return brotli_uncompress($data); // @todo: https://github.com/kjdev/php-ext-brotli
450+
case 'compress': return gzuncompress($data);
451+
case 'deflate' : return gzinflate($data);
452+
case 'gzip' : return gzdecode($data);
453+
default: return $data;
454+
}
455+
456+
}

0 commit comments

Comments
 (0)