mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-09-15 12:56:40 +00:00
Merge pull request #744 from zachborboa/master
Add automatic gzip decoding of response
This commit is contained in:
@@ -2124,6 +2124,8 @@ class Curl
|
||||
* Returns the original raw response unless a default decoder has been set.
|
||||
* If the response content-type cannot be determined:
|
||||
* Returns the original raw response.
|
||||
* If the response content-encoding is gzip:
|
||||
* Returns the response gzip-decoded.
|
||||
*/
|
||||
private function parseResponse($response_headers, $raw_response)
|
||||
{
|
||||
@@ -2148,6 +2150,13 @@ class Curl
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($response_headers['Content-Encoding']) && $response_headers['Content-Encoding'] === 'gzip') {
|
||||
$decoded_response = gzdecode($response);
|
||||
if ($decoded_response !== false) {
|
||||
$response = $decoded_response;
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
@@ -4385,4 +4385,15 @@ class CurlTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertEquals(5, $test->curl->retries);
|
||||
$this->assertFalse($test->curl->error);
|
||||
}
|
||||
|
||||
public function testGzipDecoding()
|
||||
{
|
||||
$test = new Test();
|
||||
$test->server('json_response', 'POST', [
|
||||
'key' => 'content-encoding',
|
||||
'value' => 'gzip',
|
||||
'body' => gzencode('hello'),
|
||||
]);
|
||||
$this->assertEquals('hello', $test->curl->response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,16 +197,22 @@ if ($test === 'http_basic_auth') {
|
||||
$value = 'application/json';
|
||||
}
|
||||
|
||||
if (isset($_POST['body'])) {
|
||||
$body = $_POST['body'];
|
||||
} else {
|
||||
$body = json_encode([
|
||||
'null' => null,
|
||||
'true' => true,
|
||||
'false' => false,
|
||||
'integer' => 1,
|
||||
'float' => 3.14,
|
||||
'empty' => '',
|
||||
'string' => 'string',
|
||||
]);
|
||||
}
|
||||
|
||||
header($key . ': ' . $value);
|
||||
echo json_encode([
|
||||
'null' => null,
|
||||
'true' => true,
|
||||
'false' => false,
|
||||
'integer' => 1,
|
||||
'float' => 3.14,
|
||||
'empty' => '',
|
||||
'string' => 'string',
|
||||
]);
|
||||
echo $body;
|
||||
exit;
|
||||
} elseif ($test === 'xml_response') {
|
||||
$key = $_POST['key'];
|
||||
|
||||
Reference in New Issue
Block a user