/** * Parse json data. * * @param string|ResponseInterface $body * @param bool $throws * * @throws HttpException * * @return array */ public function parseJSON($body, $throws = true) { if ($body instanceof ResponseInterface) { $body = $body->getBody(); } $contents = json_decode($body, true); if (JSON_ERROR_NONE !== json_last_error()) { Log::error('Failed to parse JSON.', ['body' => $body]); if ($throws) { throw new HttpException('Failed to parse JSON.', json_last_error()); } } return $contents; }
/** * Parse response. * * @param ResponseInterface $response * * @throws EasemobException * @throws \light\Easemob\Exception\HttpException * * @return mixed */ protected function parseResponse(ResponseInterface $response) { $statusCode = $response->getStatusCode(); if (200 !== $statusCode) { //通用false代表此次请求失败,并没有成功 Log::error('Got an error reponse:', ['__class__' => get_called_class(), 'code' => $statusCode, 'responseBody' => (string) $response->getBody()]); return false; } $result = $this->http->parseJSON($response); Log::debug('Parse response body result:', ['response' => $result]); if (isset($result['error'])) { Log::error('Got an error from server:', ['__class__' => get_called_class(), 'error' => $result]); throw new EasemobException($result['error_description']); } return $result; }