@@ -34,7 +34,6 @@ function __construct($key, $appIdentifier = NULL, $proxy = NULL) {
3434 $ userAgent = join (" " , array_filter (array (self ::userAgent (), $ appIdentifier )));
3535
3636 $ this ->options = array (
37- CURLOPT_BINARYTRANSFER => true ,
3837 CURLOPT_RETURNTRANSFER => true ,
3938 CURLOPT_HEADER => true ,
4039 CURLOPT_USERPWD => $ key ? ("api: " . $ key ) : NULL ,
@@ -114,7 +113,7 @@ function request($method, $url, $body = NULL) {
114113 $ headers = self ::parseHeaders (substr ($ response , 0 , $ headerSize ));
115114 $ responseBody = substr ($ response , $ headerSize );
116115
117- if (isset ($ headers ["compression-count " ]) ) {
116+ if ( isset ($ headers ["compression-count " ] ) ) {
118117 Tinify::setCompressionCount (intval ($ headers ["compression-count " ]));
119118 }
120119
@@ -126,6 +125,17 @@ function request($method, $url, $body = NULL) {
126125 Tinify::setPayingState ( $ headers ["paying-state " ] );
127126 }
128127
128+ $ details = json_decode ($ responseBody );
129+ if (!$ details ) {
130+ $ message = sprintf ("Error while parsing response: %s (#%d) " ,
131+ PHP_VERSION_ID >= 50500 ? json_last_error_msg () : "Error " ,
132+ json_last_error ());
133+ $ details = (object ) array (
134+ "message " => $ message ,
135+ "error " => "ParseError "
136+ );
137+ }
138+
129139 if ( isset ( $ headers ["email-address " ] ) ) {
130140 Tinify::setEmailAddress ( $ headers ["email-address " ] );
131141 }
0 commit comments