diff --git a/src/Vtiger.php b/src/Vtiger.php index d73bf59..9d8da97 100644 --- a/src/Vtiger.php +++ b/src/Vtiger.php @@ -98,7 +98,7 @@ protected function sessionId() $sessionData = $this->storeSession(); } - if (isset($json->sessionid)) { + if (isset($sessionData->sessionid)) { $sessionId = $sessionData->sessionid; } else { $sessionId = $this->login($sessionData); @@ -140,7 +140,7 @@ protected function login($sessionData) ]); // decode the response - $loginResult = json_decode($response->getBody()); + $loginResult = $this->_processResponse($response); $tryCounter++; } while (!isset($loginResult->success) && $tryCounter <= $this->maxRetries); @@ -236,7 +236,7 @@ protected function getToken() ]); $tryCounter++; - } while (!isset(json_decode($response->getBody())->success) && $tryCounter <= $this->maxRetries); + } while (!isset($this->_processResponse($response)->success) && $tryCounter <= $this->maxRetries); if ($tryCounter >= $this->maxRetries) { throw new VtigerError("Could not complete get token request within ".$this->maxRetries." tries", 6); @@ -272,7 +272,7 @@ protected function close($sessionId) } // send a request to close current connection - $response = $this->client->request('GET', $this->url, [ + $response = $this->client->request('POST', $this->url, [ 'query' => [ 'operation' => 'logout', 'sessionName' => $sessionId @@ -492,8 +492,7 @@ protected function _processResult($response) $this->_checkResponseStatusCode($response); - // decode the response - $data = json_decode($response->getBody()); + $data = $this->_processResponse($response); if (!isset($data->success)) { throw new VtigerError("Success property not set on VTiger response", 2); @@ -507,6 +506,28 @@ protected function _processResult($response) } + /** + * Get the json decoded response from either the body or the contents + * + * @param ResponseInterface $response + * + * @return object + */ + protected function _processResponse($response) + { + + // decode the response + if (!empty($response->getBody()->getContents())) { + $response->getBody()->rewind(); + $data = json_decode($response->getBody()->getContents()); + } else { + $data = json_decode($response->getBody()); + } + + return $data; + + } + /** * Check the response code to make sure it isn't anything but 200 *