From c6fb2bef900ecbd8c830a6ffa7d3660c2aa81045 Mon Sep 17 00:00:00 2001 From: tijmen Date: Thu, 8 May 2025 09:39:41 +0200 Subject: [PATCH 1/5] Use http post when retrieving result --- lib/Tinify/Source.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Tinify/Source.php b/lib/Tinify/Source.php index 304040a..77d6438 100644 --- a/lib/Tinify/Source.php +++ b/lib/Tinify/Source.php @@ -53,7 +53,7 @@ public function transform($options) { } public function result() { - $response = Tinify::getClient()->request("get", $this->url, $this->commands); + $response = Tinify::getClient()->request("post", $this->url, $this->commands); return new Result($response->headers, $response->body); } From 1c069b22853d207b3e363079d1091faf59fc16f3 Mon Sep 17 00:00:00 2001 From: tijmen Date: Thu, 8 May 2025 09:39:57 +0200 Subject: [PATCH 2/5] 1.6.4 and changelog --- CHANGES.md | 3 +++ lib/Tinify.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index b72e531..ad87373 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +## 1.6.4 +* Will use POST instead of GET when retrieving result. + ## 1.6.3 * Add minimum TLS 1.2 version to curl options as protocol negotiation on certain openssl/libcurl versions is flaky. diff --git a/lib/Tinify.php b/lib/Tinify.php index 9e34876..80f533f 100644 --- a/lib/Tinify.php +++ b/lib/Tinify.php @@ -2,7 +2,7 @@ namespace Tinify; -const VERSION = "1.6.3"; +const VERSION = "1.6.4"; class Tinify { private static $key = NULL; From ce9729d8eb6c07c16f099d2dc39048dcfd621768 Mon Sep 17 00:00:00 2001 From: tijmen Date: Mon, 12 May 2025 13:43:53 +0200 Subject: [PATCH 3/5] When commands is empty, do a get with body null --- lib/Tinify/Source.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Tinify/Source.php b/lib/Tinify/Source.php index 77d6438..97b9293 100644 --- a/lib/Tinify/Source.php +++ b/lib/Tinify/Source.php @@ -53,7 +53,9 @@ public function transform($options) { } public function result() { - $response = Tinify::getClient()->request("post", $this->url, $this->commands); + $method = empty($this->commands) ? "get" : "post"; + $body = $method === "post" ? $this->commands : null; + $response = Tinify::getClient()->request($method, $this->url, $body); return new Result($response->headers, $response->body); } From 6878a5b1464621d975abee6b14ee7c240626644b Mon Sep 17 00:00:00 2001 From: tijmen Date: Thu, 15 May 2025 14:33:53 +0200 Subject: [PATCH 4/5] Change condition to use empty --- lib/Tinify/Source.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Tinify/Source.php b/lib/Tinify/Source.php index 97b9293..73036cf 100644 --- a/lib/Tinify/Source.php +++ b/lib/Tinify/Source.php @@ -53,8 +53,9 @@ public function transform($options) { } public function result() { - $method = empty($this->commands) ? "get" : "post"; - $body = $method === "post" ? $this->commands : null; + $has_commands = empty($this->commands); + $method = $has_commands ? "get" : "post"; + $body = $has_commands ? $this->commands : null; $response = Tinify::getClient()->request($method, $this->url, $body); return new Result($response->headers, $response->body); } From 75c01c02759cf19ab251384a91b352665f1c5f7a Mon Sep 17 00:00:00 2001 From: tijmen Date: Tue, 22 Jul 2025 11:07:00 +0200 Subject: [PATCH 5/5] fix: correct HTTP method logic for Source result requests - Fix inverted boolean logic for has_commands check - Ensure GET is used when no commands are present - Ensure POST is used when commands are present - Add comprehensive tests for both GET and POST scenarios --- lib/Tinify/Source.php | 4 ++-- test/TinifySourceTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/Tinify/Source.php b/lib/Tinify/Source.php index 73036cf..ef7c6a1 100644 --- a/lib/Tinify/Source.php +++ b/lib/Tinify/Source.php @@ -53,8 +53,8 @@ public function transform($options) { } public function result() { - $has_commands = empty($this->commands); - $method = $has_commands ? "get" : "post"; + $has_commands = !empty($this->commands); + $method = $has_commands ? "post" : "get"; $body = $has_commands ? $this->commands : null; $response = Tinify::getClient()->request($method, $this->url, $body); return new Result($response->headers, $response->body); diff --git a/test/TinifySourceTest.php b/test/TinifySourceTest.php index 80c5fd8..de4584d 100644 --- a/test/TinifySourceTest.php +++ b/test/TinifySourceTest.php @@ -132,6 +132,31 @@ public function testResultShouldReturnResult() { )); $this->assertInstanceOf("Tinify\Result", Tinify\Source::fromBuffer("png file")->result()); + $this->assertSame("GET", CurlMock::last(CURLOPT_CUSTOMREQUEST)); + } + + /** + * When request does not contain commands, it should use method GET + * when it contains commands, it should have a body and method POST + */ + public function testResultWithCommandsShouldReturnResultUsingPost() { + Tinify\setKey("valid"); + + CurlMock::register("https://api.tinify.com/shrink", array( + "status" => 201, + "headers" => array("Location" => "https://api.tinify.com/some/location"), + )); + + CurlMock::register("https://api.tinify.com/some/location", array( + "status" => 200, "body" => "resized file" + )); + + $source = Tinify\Source::fromBuffer("png file")->resize(array("width" => 400)); + $result = $source->result(); + + $this->assertInstanceOf("Tinify\Result", $result); + $this->assertSame("POST", CurlMock::last(CURLOPT_CUSTOMREQUEST)); + $this->assertSame('{"resize":{"width":400}}', CurlMock::last(CURLOPT_POSTFIELDS)); } public function testPreserveShouldReturnSource() { @@ -162,6 +187,7 @@ public function testPreserveShouldReturnSourceWithData() { $this->assertSame("copyrighted file", Tinify\Source::fromBuffer("png file")->preserve("copyright", "location")->toBuffer()); $this->assertSame("{\"preserve\":[\"copyright\",\"location\"]}", CurlMock::last(CURLOPT_POSTFIELDS)); + $this->assertSame("POST", CurlMock::last(CURLOPT_CUSTOMREQUEST)); } public function testPreserveShouldReturnSourceWithDataForArray() {