client = $httpClient; $this->useHttps = ($https) ? true : false; if (null !== $baseUrl) { $this->setBaseUrl($baseUrl); } } /** * Make GET request and return Response object * * @param string $path Request path * @param array $parameters Key => Value array of query parameters * @return GuzzleResponse */ public function sendGetRequest($path, array $parameters = []) { /** @var GuzzleRequest $request */ $request = $this->client->get($path); $query = $request->getQuery(); foreach ($parameters as $parameter => $value) { $query->set($parameter, $value); } return $request->send(); } /** * Make POST request and return Response object * * @param string $path Request path * @param array $parameters Key => Value array of request data * @return GuzzleResponse */ public function sendPostRequest($path, array $parameters = []) { // Cleaning POST parameters from potential @file injections array_walk($parameters, function (string &$value, string $key) { str_replace('@', '', $value); }); /** @var GuzzleRequest $request */ $request = $this->client->post($path, null, $parameters); return $request->send(); } /** * Make GET request and return data from response * * @param string $path Path template * @param array $parameters Parameters array used to fill path template * @param bool $decodeJsonResponse Decode JSON or return plaintext * @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays * @return mixed */ public function getGetRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false) { $response = $this->sendGetRequest($path, $parameters); if ($decodeJsonResponse) { if ($decodeJsonToObjects) { return json_decode($response->getBody(true)); } else { return $response->json(); } } else { return $response->getBody(true); } } /** * Make POST request and return data from response * * @param string $path Path template * @param array $parameters Parameters array used to fill path template * @param bool $decodeJsonResponse Decode JSON or return plaintext * @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays * @return mixed */ public function getPostRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false) { $response = $this->sendPostRequest($path, $parameters); if ($decodeJsonResponse) { if ($decodeJsonToObjects) { return json_decode($response->getBody(true)); } else { return $response->json(); } } else { return $response->getBody(true); } } /** * Get HTTP client base URL * * @return string Base URL of client */ public function getBaseUrl() { return $this->client->getBaseUrl(); } /** * Set HTTP client base URL * * @param string $baseUrl Base URL of API * @param bool $useProtocol Do not change URL scheme (http/https) defined in $baseUrl * @return $this */ public function setBaseUrl($baseUrl, $useProtocol = false) { // Overriding protocol if (!$useProtocol) { $baseUrl = str_replace(['http://', 'https://',], ($this->useHttps) ? 'https://' : 'http://', $baseUrl); } // Adding missing protocol if ((false === strpos(strtolower($baseUrl), 'http://')) && (false === strpos(strtolower($baseUrl), 'https://'))) { $baseUrl = (($this->useHttps) ? 'https://' : 'http://') . $baseUrl; } $this->client->setBaseUrl($baseUrl); return $this; } /** * Check if API service uses HTTPS * * @return bool */ public function isHttps() { return $this->useHttps; } /** * Enable HTTPS * * @return $this */ public function enableHttps() { $this->useHttps = true; $this->setBaseUrl($this->getBaseUrl()); return $this; } /** * Disable HTTPS * * @return $this */ public function disableHttps() { $this->useHttps = false; $this->setBaseUrl($this->getBaseUrl()); return $this; } }