1414 */
1515class Matomo
1616{
17- public const ERROR_EMPTY = 11 ;
18-
1917 public const PERIOD_DAY = 'day ' ;
2018 public const PERIOD_WEEK = 'week ' ;
2119 public const PERIOD_MONTH = 'month ' ;
@@ -539,7 +537,7 @@ public function reset(): Matomo
539537 * @param string $method HTTP method
540538 * @param array $params Query parameters
541539 * @param array $optional Optional arguments for this api call
542- * @param string|null $format Override the response format
540+ * @param string|null $overrideFormat Override the response format
543541 *
544542 * @return bool|object
545543 * @@throws InvalidRequestException|JsonException|InvalidResponseException
@@ -549,13 +547,15 @@ private function _request(
549547 string $ method ,
550548 array $ params = [],
551549 array $ optional = [],
552- string $ format = null
550+ string $ overrideFormat = null
553551 ): mixed {
554552 $ url = $ this ->_parseUrl ($ method , $ params + $ optional );
555553 if ($ url === '' ) {
556554 throw new InvalidRequestException ('Could not parse URL! ' );
557555 }
558556
557+ $ format = $ overrideFormat ?? $ this ->_format ;
558+
559559 try {
560560 $ response = $ this ->_client ->get ($ url , [
561561 'verify ' => $ this ->_verifySsl ,
@@ -570,12 +570,14 @@ private function _request(
570570
571571 // Validate if the response was successful
572572 if ($ response ->getStatusCode () !== 200 ) {
573- throw new InvalidRequestException ($ response ->getBody (), $ response ->getStatusCode ());
573+ throw new InvalidRequestException ($ response ->getBody ()->getContents (),
574+ $ response ->getStatusCode ());
574575 }
575576
576577 // Sometimes the response was unsuccessful, but the status code was 200
577578 if ($ format === self ::FORMAT_JSON ) {
578579 $ valid = $ this ->_isValidResponse ($ response );
580+
579581 if ($ valid !== true ) {
580582 throw new InvalidResponseException ($ valid .' ( ' .$ this ->_parseUrl ($ method , $ params )
581583 .') ' , 403 );
@@ -654,22 +656,24 @@ private function _parseUrl(string $method, array $params = []): string
654656 *
655657 * @param mixed $response
656658 *
657- * @return bool|int
659+ * @return bool|string
658660 * @throws JsonException
659661 */
660- private function _isValidResponse (Response $ response ): bool |int
662+ private function _isValidResponse (Response $ response ): bool |string
661663 {
662- if (is_null ($ response ->getRawBody ())) {
663- return self ::ERROR_EMPTY ;
664+ $ body = $ response ->getBody ()->getContents ();
665+
666+ if ($ body === '' ) {
667+ return 'Empty response! ' ;
664668 }
665669
666- $ result = json_decode ($ response -> getRawBody () , true , 512 , JSON_THROW_ON_ERROR );
670+ $ result = json_decode ($ body , true , 512 , JSON_THROW_ON_ERROR );
667671
668- if ( ! isset ($ result ['result ' ]) || ( $ result ['result ' ] ! == 'error ' )) {
669- return true ;
672+ if (isset ($ result ['result ' ]) && ( strtolower ( $ result ['result ' ]) = == 'error ' )) {
673+ return $ result [ ' message ' ] ;
670674 }
671675
672- return $ result [ ' message ' ] ;
676+ return true ;
673677 }
674678
675679 /**
@@ -686,7 +690,8 @@ private function _parseResponse(Response $response, string $overrideFormat = nul
686690 $ format = $ overrideFormat ?? $ this ->_format ;
687691
688692 return match ($ format ) {
689- self ::FORMAT_JSON => json_decode ($ response ->getBody (), $ this ->_isJsonDecodeAssoc , 512 ,
693+ self ::FORMAT_JSON => json_decode ($ response ->getBody ()->getContents (),
694+ $ this ->_isJsonDecodeAssoc , 512 ,
690695 JSON_THROW_ON_ERROR ),
691696 default => $ response ,
692697 };
0 commit comments