diff --git a/README.md b/README.md index bfaed64..2239c10 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,8 @@ Example config: 'timeout' => 3, // the token must match 'upload.token' config in XHGui 'token' => 'token', + // whether to gzip compress the payload + 'compress' => true, ), ``` diff --git a/src/Saver/UploadSaver.php b/src/Saver/UploadSaver.php index 38fbbda..f05743d 100644 --- a/src/Saver/UploadSaver.php +++ b/src/Saver/UploadSaver.php @@ -10,8 +10,10 @@ class UploadSaver implements SaverInterface private $url; /** @var int */ private $timeout; + /** @var bool */ + private $compress; - public function __construct($url, $token, $timeout) + public function __construct($url, $token, $timeout, $compress) { $this->url = $url; if ($token) { @@ -19,6 +21,7 @@ public function __construct($url, $token, $timeout) } $this->timeout = $timeout; + $this->compress = $compress; } public function isSupported() @@ -29,7 +32,7 @@ public function isSupported() public function save(array $data) { $json = json_encode($data); - $this->submit($this->url, $json); + $this->submit($this->url, $json, $this->hasCompression()); return true; } @@ -37,8 +40,9 @@ public function save(array $data) /** * @param string $url * @param string $payload + * @param bool $compress */ - private function submit($url, $payload) + private function submit($url, $payload, $compress) { $ch = curl_init($url); if (!$ch) { @@ -49,13 +53,13 @@ private function submit($url, $payload) // Prefer to receive JSON back 'Accept: application/json', // The sent data is JSON - 'Content-Type: application/json', + $compress ? 'Content-Type: application/json+gzip' : 'Content-Type: application/json', ); $res = curl_setopt_array($ch, array( CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_RETURNTRANSFER => true, - CURLOPT_POSTFIELDS => $payload, + CURLOPT_POSTFIELDS => $compress ? gzencode($payload) : $payload, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_HTTPHEADER => $headers, CURLOPT_TIMEOUT => $this->timeout, @@ -80,4 +84,12 @@ private function submit($url, $payload) throw new ProfilerException($message); } } + + /** + * @return bool + */ + private function hasCompression() + { + return $this->compress && function_exists('gzencode'); + } } diff --git a/src/SaverFactory.php b/src/SaverFactory.php index 9a0dd6f..5d22b11 100644 --- a/src/SaverFactory.php +++ b/src/SaverFactory.php @@ -32,10 +32,11 @@ public static function create($saveHandler, array $config = array()) 'url' => null, 'token' => null, 'timeout' => 3, + 'compress' => false, ); $userConfig = isset($config['save.handler.upload']) && is_array($config['save.handler.upload']) ? $config['save.handler.upload'] : array(); $saverConfig = array_merge($defaultConfig, $userConfig); - $saver = new Saver\UploadSaver($saverConfig['url'] ?: $saverConfig['uri'], $saverConfig['token'], $saverConfig['timeout']); + $saver = new Saver\UploadSaver($saverConfig['url'] ?: $saverConfig['uri'], $saverConfig['token'], $saverConfig['timeout'], $saverConfig['compress']); break; case Profiler::SAVER_STACK: