private function sendRequest() { $request = $this->getRequest(); if ($this->multipartStreamBuilder !== null) { $request = $request->withBody($this->multipartStreamBuilder->build())->withHeader('Content-Type', 'multipart/form-data;boundary=' . $this->multipartStreamBuilder->getBoundary()); } try { $this->response = $this->client->sendRequest($request); } catch (HttpException $e) { $this->response = $e->getResponse(); if ($this->response === null) { throw $e; } } }
/** * @param string $method * @param string $uri * @param mixed $body * @param array $files * @param array $headers * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters * * @return \stdClass */ protected function send($method, $uri, $body = null, $files = [], array $headers = []) { $headers['User-Agent'] = Api::SDK_USER_AGENT . '/' . Api::SDK_VERSION; $headers['Authorization'] = 'Basic ' . base64_encode(sprintf('%s:%s', Api::API_USER, $this->apiKey)); if (!empty($files)) { $builder = new MultipartStreamBuilder(); foreach ($files as $file) { $builder->addResource($file['name'], $file['contents'], $file); } $body = $builder->build(); $headers['Content-Type'] = 'multipart/form-data; boundary=' . $builder->getBoundary(); } elseif (is_array($body)) { $body = http_build_query($body); $headers['Content-Type'] = 'application/x-www-form-urlencoded'; } $request = MessageFactoryDiscovery::find()->createRequest($method, $this->getApiUrl($uri), $headers, $body); $response = $this->getHttpClient()->sendRequest($request); return $this->responseHandler($response); }
/** * @param string $method * @param string $uri * @param array $headers * @param array $body * * @return \Psr\Http\Message\ResponseInterface */ private function sendMultipartRequest(string $method, string $uri, array $headers, array $body) { $builder = new MultipartStreamBuilder($this->streamFactory); foreach ($body as $item) { $builder->addResource($item['name'], $item['contents']); } $multipartStream = $builder->build(); $boundary = $builder->getBoundary(); $request = $this->messageFactory->createRequest($method, $uri, array_merge(['Content-Type' => 'multipart/form-data; boundary=' . $boundary], $headers), $multipartStream); return $this->getHttpClient()->sendRequest($request); }