Skip to content

Commit 75d1cd1

Browse files
author
Edwin Westerhoud
committed
Updated tinify-php client.
1 parent 5ed7f32 commit 75d1cd1

3 files changed

Lines changed: 70 additions & 58 deletions

File tree

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vendor/tinify/Tinify.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Tinify;
44

5-
const VERSION = "1.4.0";
5+
const VERSION = "1.5.0";
66

77
class Tinify {
88
const AUTHENTICATED = true;

src/vendor/tinify/Tinify/Client.php

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
class Client {
66
const API_ENDPOINT = "https://api.tinify.com";
77

8+
const RETRY_COUNT = 1;
9+
const RETRY_DELAY = 500;
10+
811
protected $options;
912

1013
public static function userAgent() {
@@ -64,73 +67,82 @@ function request($method, $url, $body = NULL) {
6467
}
6568
}
6669

67-
$request = curl_init();
68-
if ($request === false || $request === null) {
69-
throw new ConnectionException(
70-
"Error while connecting: curl extension is not functional or disabled."
71-
);
72-
}
70+
for ($retries = self::RETRY_COUNT; $retries >= 0; $retries--) {
71+
if ($retries < self::RETRY_COUNT) {
72+
usleep(self::RETRY_DELAY * 1000);
73+
}
7374

74-
curl_setopt_array($request, $this->options);
75+
$request = curl_init();
76+
if ($request === false || $request === null) {
77+
throw new ConnectionException(
78+
"Error while connecting: curl extension is not functional or disabled."
79+
);
80+
}
7581

76-
$url = strtolower(substr($url, 0, 6)) == "https:" ? $url : self::API_ENDPOINT . $url;
77-
curl_setopt($request, CURLOPT_URL, $url);
78-
curl_setopt($request, CURLOPT_CUSTOMREQUEST, strtoupper($method));
82+
curl_setopt_array($request, $this->options);
7983

80-
if (count($header) > 0) {
81-
curl_setopt($request, CURLOPT_HTTPHEADER, $header);
82-
}
84+
$url = strtolower(substr($url, 0, 6)) == "https:" ? $url : self::API_ENDPOINT . $url;
85+
curl_setopt($request, CURLOPT_URL, $url);
86+
curl_setopt($request, CURLOPT_CUSTOMREQUEST, strtoupper($method));
8387

84-
if ($body) {
85-
curl_setopt($request, CURLOPT_POSTFIELDS, $body);
86-
}
88+
if (count($header) > 0) {
89+
curl_setopt($request, CURLOPT_HTTPHEADER, $header);
90+
}
8791

88-
$response = curl_exec($request);
92+
if ($body) {
93+
curl_setopt($request, CURLOPT_POSTFIELDS, $body);
94+
}
8995

90-
if (is_string($response)) {
91-
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
92-
$headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE);
93-
curl_close($request);
96+
$response = curl_exec($request);
9497

95-
$headers = self::parseHeaders(substr($response, 0, $headerSize));
96-
$body = substr($response, $headerSize);
98+
if (is_string($response)) {
99+
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
100+
$headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE);
101+
curl_close($request);
97102

98-
if (isset($headers["compression-count"])) {
99-
Tinify::setCompressionCount(intval($headers["compression-count"]));
100-
}
103+
$headers = self::parseHeaders(substr($response, 0, $headerSize));
104+
$body = substr($response, $headerSize);
101105

102-
$isJson = false;
103-
if (isset($headers["content-type"])) {
104-
/* Parse JSON response bodies. */
105-
list($contentType) = explode(";", $headers["content-type"], 2);
106-
if (strtolower(trim($contentType)) == "application/json") {
107-
$isJson = true;
106+
if (isset($headers["compression-count"])) {
107+
Tinify::setCompressionCount(intval($headers["compression-count"]));
108108
}
109-
}
110109

111-
/* 1xx and 3xx are unexpected and will be treated as error. */
112-
$isError = $status <= 199 || $status >= 300;
113-
114-
if ($isJson || $isError) {
115-
/* Parse JSON bodies, always interpret errors as JSON. */
116-
$body = json_decode($body);
117-
if (!$body) {
118-
$message = sprintf("Error while parsing response: %s (#%d)",
119-
PHP_VERSION_ID >= 50500 ? json_last_error_msg() : "Error",
120-
json_last_error());
121-
throw Exception::create($message, "ParseError", $status);
110+
$isJson = false;
111+
if (isset($headers["content-type"])) {
112+
/* Parse JSON response bodies. */
113+
list($contentType) = explode(";", $headers["content-type"], 2);
114+
if (strtolower(trim($contentType)) == "application/json") {
115+
$isJson = true;
116+
}
122117
}
123-
}
124118

125-
if ($isError) {
126-
throw Exception::create($body->message, $body->error, $status);
127-
}
119+
/* 1xx and 3xx are unexpected and will be treated as error. */
120+
$isError = $status <= 199 || $status >= 300;
121+
122+
if ($isJson || $isError) {
123+
/* Parse JSON bodies, always interpret errors as JSON. */
124+
$body = json_decode($body);
125+
if (!$body) {
126+
$message = sprintf("Error while parsing response: %s (#%d)",
127+
PHP_VERSION_ID >= 50500 ? json_last_error_msg() : "Error",
128+
json_last_error());
129+
if ($retries > 0 && $status >= 500) continue;
130+
throw Exception::create($message, "ParseError", $status);
131+
}
132+
}
128133

129-
return (object) array("body" => $body, "headers" => $headers);
130-
} else {
131-
$message = sprintf("%s (#%d)", curl_error($request), curl_errno($request));
132-
curl_close($request);
133-
throw new ConnectionException("Error while connecting: " . $message);
134+
if ($isError) {
135+
if ($retries > 0 && $status >= 500) continue;
136+
throw Exception::create($body->message, $body->error, $status);
137+
}
138+
139+
return (object) array("body" => $body, "headers" => $headers);
140+
} else {
141+
$message = sprintf("%s (#%d)", curl_error($request), curl_errno($request));
142+
curl_close($request);
143+
if ($retries > 0) continue;
144+
throw new ConnectionException("Error while connecting: " . $message);
145+
}
134146
}
135147
}
136148

0 commit comments

Comments
 (0)