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; diff --git a/lib/Tinify/Source.php b/lib/Tinify/Source.php index 304040a..ef7c6a1 100644 --- a/lib/Tinify/Source.php +++ b/lib/Tinify/Source.php @@ -53,7 +53,10 @@ public function transform($options) { } public function result() { - $response = Tinify::getClient()->request("get", $this->url, $this->commands); + $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() {