Send a Guzzle request.

Using this method allows exceptions to be standardized.

@param RequestInterface $request
@param ClientInterface  $client
@param array            $options

@internal

@return array
519615
public static function send(RequestInterface $request, ClientInterface $client, array $options = [])
    {
        $response = null;
        try {
            $response = $client->send($request, $options);
            $body = $response->getBody()->getContents();
            $data = [];
            if ($body) {
                $response->getBody()->seek(0);
                $body = $response->getBody()->getContents();
                $data = \GuzzleHttp\json_decode($body, true);
            }

            return (array) $data;
        } catch (BadResponseException $e) {
            throw ApiResponseException::create($e->getRequest(), $e->getResponse());
        } catch (\InvalidArgumentException $e) {
            throw ApiResponseException::create($request, $response);
        }
    }
35253
public function call(string $method, RequestInterface $request)
    {
        try {
            $parameters = $request->toArray();

            return $this->__soapCall($method, ['parameters' => $parameters]);
        } catch (\Exception $exception) {
            if ($this->debug) {
                throw ClientException::create($this, $exception);
            }

            throw new ClientException($exception->getMessage(), $exception->getCode(), $exception);
        }
    }
1163959
public function sendRequest(Request\RequestInterface $request, array $options = [])
    {
        // cookies 必须启用，如果不设置则使用当前凭证的cookie.
        if (!isset($options['cookies']) && $this->credential) {
            $options['cookies'] = $this->credential->getCookies();
        }
        if ($parameters = $request->getParameters()) {
            if (Request\RequestInterface::REQUEST_METHOD_GET == $request->getMethod()) {
                $options['query'] = $parameters;
            } else {
                $options['form_params'] = $parameters;
            }
        }
        //如果有referer需要伪造该信息
        if ($referer = $request->getReferer()) {
            $options['headers'] = [
                'Referer' => $referer,
            ];
        }
        $response = $this->httpClient->send(
            new HttpRequest($request->getMethod(), $request->getUri()),
            $options
        );

        return $response;
    }
561290
public function getExceptionFor(RequestInterface $request): ClientExceptionInterface
    {
        try {
            return $this->exceptions[$request];
        } catch (\UnexpectedValueException $e) {
            throw new \UnexpectedValueException('Request not found', $e->getCode(), $e);
        }
    }
487323
public function execute(Client $client)
    {
        if (!$this->request instanceof Request) {
            throw new \InvalidArgumentException('request is not an instanceof \GuzzleHttp\Message\Request');
        }

        $this->copyClientDefaults($client);

        return $client->send($this->request);
    }
781892
public function appyAuthorization(RequestInterface $request, array $options = null)
    {
        // Only sign requests using "auth"="oauth"
        // IMPORTANT: you have to create special auth client (GuzzleHttp\Client) if you want to get all the request authorized
        if (!isset($options['auth']) || $options['auth'] != 'oauth') {
            return $request;
        }

        // get Access token for current request
        $accessToken = $this->getAccessToken();
        if (is_string($accessToken)) {
            return $request->withHeader('Authorization', 'Bearer ' . $accessToken);
        } else {
            throw new ClientError('Authentication error, invalid on no access token data returned.');
        }
    }
12976
private function _request($method, $url, array $options) {
		try {
			$response = $this->httpClient->{$method}($url, $options);
		} catch(ClientException $e) {
			$response = $e->getResponse();
		} catch(ConnectException $e) {
			$this->_throwServerError($e);
		} catch (RequestException $e) {
			$this->_throwServerError($e);
		}

		return $response;
	}
226125
public function send(RequestInterface $request)
    {
        /** @var RequestEvent $event */
        $event = new RequestEvent($request);
        $event = $this->eventDispatcher->dispatch(ClientEvents::REQUEST, $event);

        $request = $event->getRequest();

        $response = $this->httpClient->send($request);

        /** @var ResponseEvent $event */
        $event = new ResponseEvent($response);
        $event = $this->eventDispatcher->dispatch(ClientEvents::RESPONSE, $event);

        return $event->getResponse();
    }
279789
public function request(RequestInterface $request, array $options = []): HttpResultInterface
    {
        $options = $this->handleOptions(array_merge($this->defaultOptions, $options));

        $url = (string)$request->getUri();
        $profileKey = 'HttpClient.' . $url;

        App::profileStart($profileKey);

        list($host, $port) = $this->ipResolve($request, $options);

        $ssl = $request->getUri()->getScheme() === 'https';

        $client = new CoHttpClient($host, $port, $ssl);
        $this->applyOptions($client, $request, $options);
        $this->applyMethod($client, $request, $options);

        $path = $request->getUri()->getPath();
        $query = $request->getUri()->getQuery();
        if ($path === '') $path = '/';
        if ($query !== '') $path .= '?' . $query;

        $client->setDefer();
        $client->execute($path);

        App::profileEnd($profileKey);

        if (null !== $client->errCode && $client->errCode !== 0) {
            App::error(sprintf('HttpClient Request ERROR #%s url=%s', $client->errCode, $url));
            throw new RuntimeException(\socket_strerror($client->errCode), $client->errCode);
        }

        $result = new HttpCoResult(null, $client, $profileKey);
        return $result;
    }
658278
protected function sendRequest(ApiRequestInterface $request)
    {
        $clientOptions = [];

        //add mock to client config
        if ($this->isModeMock()) {
            if ($request->getMock()) {
                $mockHandler = new MockHandler($request->getMock());
                $clientOptions['handler'] = $mockHandler;
            } else {
                throw new ApiException('The service can\'t be mocked, should create a valid mock for this service.');
            }
        }

        return $this->buildClient($clientOptions)->send($request, $request->getOptions()->getAll());
    }
1030318
public static function customerCount(array $options = [], ClientInterface $client = null)
    {
        return Metrics\CustomerCounts::all($options, $client);
    }
738305
protected function createHeaders(RequestInterface $request, array $options)
	{
		$headers = [];
		$body = $request->getBody();
		$size = $body->getSize();

		foreach ($request->getHeaders() as $header => $values)
		{
			foreach ((array) $values as $value)
			{
				$headers[] = sprintf('%s: %s', $header, $value);
			}
		}

		if ( ! $request->hasHeader('Transfer-Encoding') && $size === null)
		{
			$headers[] = 'Transfer-Encoding: chunked';
		}

		if ( ! $request->hasHeader('Expect') && in_array($request->getMethod(), ['POST', 'PUT']))
		{
			if ($request->getProtocolVersion() < 2.0 && ! $body->isSeekable() || $size === null || $size > 1048576)
			{
				$headers[] = 'Expect: 100-Continue';
			}
			else
			{
				$headers[] = 'Expect: ';
			}
		}

		return $headers;
	}
1080922
public function request($method, $uri = '', array $options = [])
    {
        //Always prepend the api key.

        if (isset($options['query'])) {
            $options['query']['api_key'] = $this->apiKey;
        } else {
            $options['query'] = ['api_key' => $this->apiKey];
        }

        return $this->guzzleClient->request($method, $uri, $options);
    }
42398
public function send(RequestInterface $request)
    {
        $this->logger->info(sprintf(
            '%s "%s"',
            $request->getMethod(),
            $request->getUri()
        ));

        $this->logger->debug(sprintf("Request:\n%s", (string) $request->getUri()));

        try {
            $response = $this->client->send($request);
        } catch (BadResponseException $e) {
            throw $e;
        }

        return $response;
    }
808413
protected function beforeSend(RequestInterface $request)
    {
        try {
            $state = $request->setState(RequestInterface::STATE_TRANSFER);
            if ($state == RequestInterface::STATE_TRANSFER) {
                // Add the request curl handle to the multi handle
                $this->checkCurlResult(curl_multi_add_handle($this->multiHandle, $this->createCurlHandle($request)->getHandle()));
            } else {
                // Requests might decide they don't need to be sent just before transfer (e.g. CachePlugin)
                $this->remove($request);
                if ($state == RequestInterface::STATE_COMPLETE) {
                    $this->successful[] = $request;
                }
            }
        } catch (\Exception $e) {
            // Queue the exception to be thrown when sent
            $this->removeErroredRequest($request, $e);
        }
    }
117999
public function send(): ClientRequestInterface
   {
       try {
           $this->response = new ClientResponse(
               new ClientResponseStream($this));
       } catch (InvalidArgumentException $e) {
           throw new RuntimeException(
               "Can't send client request: " . $e->getMessage());
       }
       return $this;
   }
1315146
protected function sendErrorEvent(RequestInterface $request, $reason)
    {
        $key = $this->getEventKey($request);
        $event = $this->events[$key];

        unset($this->events[$key]);

        $event->setExecutionStop();
        $event->setReason($reason);
        $this->eventDispatcher->dispatch(GuzzleHttpEvent::EVENT_ERROR_NAME, $event);
    }