/** * {@inheritdoc} * @throws JsonException */ public function sendRequest($method, $parameters = [], $options = []) { $type = 'GET'; if (array_key_exists('type', $options)) { $allowed_types = ['GET', 'POST']; if (!in_array($options['type'], $allowed_types)) { throw new InvalidArgumentException('Invalid request type ' . $options['type']); } else { $type = $options['type']; } } $method_name = ucfirst($method); $request_parameters = array_merge(['method' => $method_name, 'token' => $this->api_key], $parameters); try { if ('GET' === $type) { $response = $this->client->get($this->url, ['query' => $request_parameters, 'connect_timeout' => 10]); } else { $response = $this->client->post($this->url, ['form_params' => $request_parameters, 'connect_timeout' => 10]); } $json_data = $response->getBody()->getContents(); } catch (\GuzzleHttp\Exception\GuzzleException $error) { throw new ApiException($error->getMessage(), ApiException::HTTP_ERROR, $error); } //Удаление BOM в выводе если есть $json_data = preg_replace('~^\\xEF\\xBB\\xBF~isu', '', $json_data); $data = json_decode($json_data, false); //В случае неудачи разбора JSON кидаем исключение JsonException if (NULL === $data) { throw new JsonException('Failed to parse received json with message ' . json_last_error_msg(), JsonException::DECODE_EXCEPTION, null, $json_data); } //Если API вернуло какую-то ошибку if (is_array($data) && isset($data[0]) && isset($data[0]->err)) { throw new ApiException('Errors occured while processing API request', ApiException::API_ERROR, null, $data[0]->err); } elseif (is_object($data) && isset($data->err)) { throw new ApiException('Errors occured while processing API request', ApiException::API_ERROR, null, $data->err); } return $data; }
/** * @param EventModel $event * @param $userID * @param string $language * @return \Psr\Http\Message\ResponseInterface */ public function createTalk(EventModel $event, $userID, $language = 'English - UK') { $response = $this->httpClient->post($this->joindinEvent->getUrl('events/' . $this->joindinEvent->getJoindinEventID() . '/talks'), ['json' => $this->joindinEvent->getCreateEventTitlePayload($event, $language), 'headers' => $this->joindinEvent->getHeaders($userID)]); $this->joindinEvent->setTalkLocation($response->getHeader('location')[0]); return $response; }