Skip to content

Commit 2bc5bee

Browse files
committed
Add support for compressing post payload
1 parent 112ec60 commit 2bc5bee

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/Saver/UploadSaver.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ class UploadSaver implements SaverInterface
1010
private $url;
1111
/** @var int */
1212
private $timeout;
13+
/** @var bool */
14+
private $compress;
1315

14-
public function __construct($url, $token, $timeout)
16+
public function __construct($url, $token, $timeout, $compress = false)
1517
{
1618
$this->url = $url;
1719
if ($token) {
1820
$this->url .= '?&token=' . $token;
1921
}
2022

2123
$this->timeout = $timeout;
24+
$this->compress = $compress;
2225
}
2326

2427
public function isSupported()
@@ -29,16 +32,17 @@ public function isSupported()
2932
public function save(array $data)
3033
{
3134
$json = json_encode($data);
32-
$this->submit($this->url, $json);
35+
$this->submit($this->url, $json, $this->compress);
3336

3437
return true;
3538
}
3639

3740
/**
3841
* @param string $url
3942
* @param string $payload
43+
* @param bool $compress
4044
*/
41-
private function submit($url, $payload)
45+
private function submit($url, $payload, $compress)
4246
{
4347
$ch = curl_init($url);
4448
if (!$ch) {
@@ -49,13 +53,13 @@ private function submit($url, $payload)
4953
// Prefer to receive JSON back
5054
'Accept: application/json',
5155
// The sent data is JSON
52-
'Content-Type: application/json',
56+
$compress ? 'Content-Type: application/json+gzip' : 'Content-Type: application/json',
5357
);
5458

5559
$res = curl_setopt_array($ch, array(
5660
CURLOPT_CUSTOMREQUEST => 'POST',
5761
CURLOPT_RETURNTRANSFER => true,
58-
CURLOPT_POSTFIELDS => $payload,
62+
CURLOPT_POSTFIELDS => $compress ? gzencode($payload) : $payload,
5963
CURLOPT_FOLLOWLOCATION => 1,
6064
CURLOPT_HTTPHEADER => $headers,
6165
CURLOPT_TIMEOUT => $this->timeout,

0 commit comments

Comments
 (0)