Send the HTTP request and return an HTTP response object

@param string $method
@return Zend_Http_Response
@throws Zend_Http_Client_Exception
773971
public function request($method = null)
    {
        if (! $this->uri instanceof Zend_Uri_Http) {
            /** @see Zend_Http_Client_Exception */

            throw new Zend_Http_Client_Exception('No valid URI has been passed to the client');
        }

        if ($method) {
            $this->setMethod($method);
        }
        $this->redirectCounter = 0;
        $response = null;

        // Make sure the adapter is loaded
        if ($this->adapter == null) {
            $this->setAdapter($this->config['adapter']);
        }

        // Send the first request. If redirected, continue.
        do {
            // Clone the URI and add the additional GET parameters to it
            $uri = clone $this->uri;
            if (! empty($this->paramsGet)) {
                $query = $uri->getQuery();
                   if (! empty($query)) {
                       $query .= '&';
                   }
                $query .= http_build_query($this->paramsGet, null, '&');
                if ($this->config['rfc3986_strict']) {
                    $query = str_replace('+', '%20', $query);
                }

                // @see ZF-11671 to unmask for some services to foo=val1&foo=val2
                if ($this->getUnmaskStatus()) {
                    if ($this->_queryBracketsEscaped) {
                        $query = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query);
                    } else {
                        $query = preg_replace('/\\[(?:[0-9]|[1-9][0-9]+)\\]=/', '=', $query);
                    }
                }

                $uri->setQuery($query);
            }

            $body = $this->_prepareBody();
            $headers = $this->_prepareHeaders();

            // check that adapter supports streaming before using it
            if(is_resource($body) && !($this->adapter instanceof Zend_Http_Client_Adapter_Stream)) {
                /** @see Zend_Http_Client_Exception */

                throw new Zend_Http_Client_Exception('Adapter does not support streaming');
            }

            // Open the connection, send the request and read the response
            $this->adapter->connect($uri->getHost(), $uri->getPort(),
                ($uri->getScheme() == 'https' ? true : false));

            if($this->config['output_stream']) {
                if($this->adapter instanceof Zend_Http_Client_Adapter_Stream) {
                    $stream = $this->_openTempStream();
                    $this->adapter->setOutputStream($stream);
                } else {
                    /** @see Zend_Http_Client_Exception */

                    throw new Zend_Http_Client_Exception('Adapter does not support streaming');
                }
            }

            $this->last_request = $this->adapter->write($this->method,
                $uri, $this->config['httpversion'], $headers, $body);

            $response = $this->adapter->read();
            if (! $response) {
                /** @see Zend_Http_Client_Exception */

                throw new Zend_Http_Client_Exception('Unable to read response, or response is empty');
            }

            if($this->config['output_stream']) {
                $streamMetaData = stream_get_meta_data($stream);
                if ($streamMetaData['seekable']) {
                    rewind($stream);
                }
                // cleanup the adapter
                $this->adapter->setOutputStream(null);
                $response = Zend_Http_Response_Stream::fromStream($response, $stream);
                $response->setStreamName($this->_stream_name);
                if(!is_string($this->config['output_stream'])) {
                    // we used temp name, will need to clean up
                    $response->setCleanup(true);
                }
            } else {
                $response = Zend_Http_Response::fromString($response);
            }

            if ($this->config['storeresponse']) {
                $this->last_response = $response;
            }

            // Load cookies into cookie jar
            if (isset($this->cookiejar)) {
                $this->cookiejar->addCookiesFromResponse($response, $uri, $this->config['encodecookies']);
            }

            // If we got redirected, look for the Location header
            if ($response->isRedirect() && ($location = $response->getHeader('location'))) {

                // Avoid problems with buggy servers that add whitespace at the
                // end of some headers (See ZF-11283)
                $location = trim($location);

                // Check whether we send the exact same request again, or drop the parameters
                // and send a GET request
                if ($response->getStatus() == 303 ||
                   ((! $this->config['strictredirects']) && ($response->getStatus() == 302 ||
                       $response->getStatus() == 301))) {

                    $this->resetParameters();
                    $this->setMethod(self::GET);
                }

                // If we got a well formed absolute URI
                if (($scheme = substr($location, 0, 6)) && ($scheme == 'http:/' || $scheme == 'https:')) {
                    $this->setHeaders('host', null);
                    $this->setUri($location);

                } else {

                    // Split into path and query and set the query
                    if (strpos($location, '?') !== false) {
                        list($location, $query) = explode('?', $location, 2);
                    } else {
                        $query = '';
                    }
                    $this->uri->setQuery($query);

                    // Else, if we got just an absolute path, set it
                    if(strpos($location, '/') === 0) {
                        $this->uri->setPath($location);

                        // Else, assume we have a relative path
                    } else {
                        // Get the current path directory, removing any trailing slashes
                        $path = $this->uri->getPath();
                        $path = rtrim(substr($path, 0, strrpos($path, '/')), "/");
                        $this->uri->setPath($path . '/' . $location);
                    }
                }
                ++$this->redirectCounter;

            } else {
                // If we didn't get any location, stop redirecting
                break;
            }

        } while ($this->redirectCounter < $this->config['maxredirects']);

        return $response;
    }
338033
public function makeRequest($url, $method = 'GET')
    {
        try {
            [$url, $parameters] = $this->getRequestUrlAndParameters($url, $method === 'GET');
            return $this->response = $this->client->request($method, $url, $parameters);
        } catch (RequestException $e) {
            $this->printErrorMessage(Psr7\str($e->getRequest()), false);
            if ($e->hasResponse()) {
                $this->response = $e->getResponse();
                $this->printErrorMessage(Psr7\str($e->getResponse()), false);
            }
        }
        return false;
    }
340710
private function request($method, $path = '', array $options = [])
    {
        $options['headers'] = array_merge(
            [
                'User-Agent' => $this->app_client,
            ],
            isset($options['headers']) ? $options['headers'] : []
        );

        try {
            $response = $this->client->request($method, $this->host.$this->prefix.$path, $options);
        } catch (\Exception $e) {
            throw $this->wrapException($e);
        }

        $content = $response->getBody()->getContents();
        $content = $this->detector->detect($content);

        return $content;
    }
951297
public function call($method = 'GET', $uri = null, $headers = null, $body = null, array $options = array(), $classToUnserialize = null)
    {
        $request = $this->client->createRequest($method, $uri, $headers, $body, $options);

        if ($classToUnserialize) {
            return $this->serializer->deserialize(
                (string) $this->send($request)->getBody(),
                $classToUnserialize,
                'xml'
            );
        }

        return $this->send($request);
    }
1035901
public function request($method, array $params = [])
    {
        $client = new \GuzzleHttp\Client();

        $multipartParams = [];
        foreach ($params as $key => $value) {
            $multipartParams[] = [
                'name' => $key,
                'contents' => is_scalar($value) ? (string)$value : $value
            ];
        }

        try {
            $response = $client->post($this->getUrl($method), [
                'verify' => false,
                'multipart' => $multipartParams ?: null
            ]);
            $response = json_decode($response->getBody());

            if ($response === null) {
                throw new HttpRuntimeException('Empty response.');
            }
            return new Response($response->ok, $response->result);
        } catch (ClientException $e) {
            $response = json_decode($e->getResponse()->getBody()->getContents());
            return new Response($response->ok, null, $response->error_code, $response->description);
        } catch (ServerException $e) {
            return new Response(false, null, $e->getResponse()->getStatusCode(), $e->getResponse()->getReasonPhrase());
        }
    }
1213052
public function remoteHttpCall($method, array $args)
    {
        $reqParams = [
            'version'   => static::$version,
            'args'      => array_values($args),
            'time'      => microtime(true),
            'handler'   => $this->handler,
            'construct' => $this->construct,
            'method'    => $method
        ];
        $sendData = [
            'data' => getInstance()->pack->pack($reqParams),
        ];
        $headers = [
            'X-RPC' => 1,
        ];

        /**
         * @var Client $client
         */
        $client = yield $this->getObject(Client::class)->goDnsLookup($this->host, $this->timeout, $headers);
        if (!$client) {
            throw new Exception($this->host . ' dns query failed');
        }

        $urlPath = '/' .$this->urlPath . '/' . $this->service . '/' . $this->handler . '/' . $this->method;
        if ($this->verb == 'POST') {
            $response = yield $client->goPost($urlPath, $sendData, $this->timeout);
        } else {
            $response = yield $client->goGet($urlPath, $sendData, $this->timeout);
        }

        if ($response && !empty($response['body']) && ($jsonRes = json_decode($response['body'], true))) {
            if ($response['statusCode'] !== 200) {
                $this->getContext()->getLog()->warning(dump($response, false, true));
                return false;
            } else {
                return $jsonRes;
            }
        } else {
            $this->getContext()->getLog()->warning(dump($response, false, true));
            return false;
        }
    }
289059
protected function request($url, $method = 'GET', array $data = [])
    {
        try {
            return call_user_func_array([$this, strtolower($method)], [$url, $data]);
        } catch (\Exception $exception) {
            throw new \RuntimeException('Http method is not valid.');
        }
    }
108792
public function withMethod($method)
    {
        $this->assertMethod($method);
        
        $request = $this->copy();
        $request->method = $method;
        
        return $request;
    }
800246
public function send(string $method, string $url, array $options): HttpResponse
    {
        try {
            return new HttpResponse($this->buildClient()->request($method, $url, $this->mergeOptions([
                'query' => $this->parseQueryParams($url),
            ], $options)));
        } catch (\GuzzleHttp\Exception\ConnectException $e) {
            throw new Exceptions\ConnectionException($e->getMessage(), 0, $e);
        }
    }
901466
protected function _sendRawRequest($url, $method = Request::METHOD_GET, $body = '', \Closure $initializeRequest = null)
    {
        $logSeverity = SolrLogManager::INFO;
        $exception = null;

        try {
            $request = $this->buildSolariumRequestFromUrl($url, $method);

            if($initializeRequest !== null) {
                $request = $initializeRequest($request);
            }

            $response = $this->executeRequest($request);
        } catch (HttpException $exception) {
            $logSeverity = SolrLogManager::ERROR;
            $response = new ResponseAdapter($exception->getBody(), $exception->getCode(), $exception->getMessage());
        }

        if ($this->configuration->getLoggingQueryRawPost() || $response->getHttpStatus() != 200) {
            $message = 'Querying Solr using '.$method;
            $this->writeLog($logSeverity, $message, $url, $response, $exception, $body);
        }

        return $response;
    }
1128152
private function redirect($method) {

		$this -> setUrl();
		$this -> setDomain();
		$this -> setMethod($method);
		
		Router :: setRedirect(true);

		return Router :: execute();
	}
602437
public function callAction($method, $parameters)
    {
        $route = app('router')->current();
        $request = app('request');
        $this->assignAfter($route, $request, $method);
        $response = $this->before($route, $request, $method);

        if (is_null($response)) {
            $response = call_user_func_array([$this, $method], $parameters);
        }

        return $response;
    }
1236026
public function call(string $method, array $arguments): ResponseInterface
    {
        try {
            return $this->__soapCall($method, $arguments, null, $this->getHeader());
        } catch (\Exception $e) {
            $exception = new ClientException($e->getMessage(), $e->getCode(), $e);

            if ($this->debug) {
                $exception->request = ClientException::formatXml($this->__getLastRequest());
                $exception->response = ClientException::formatXml($this->__getLastResponse());
            }

            throw $exception;
        }
    }
1244851
public function request($method, $params = [])
    {
        $response = $this->client->post($this->getUrl($method), ['query' => $params]);

        if ($response->getStatusCode() === 200) {
            return (string)$response->getBody();
        } else {
            throw new Exception(sprintf('Sms.ru problem. Status code is %s', $response->getStatusCode()), $response->getStatusCode());
        }
    }
717272
protected function request($method, $url, $parameters = array())
    {
        Logger::get()->debug('HTTP: starting request...');
        $headers = $this->headers;

        // Add accepted content type header.
        $headers[] = 'Content-Type: ' . $this->contentType;
        $headers[] = 'Accept: ' . $this->acceptedContentType;

        $handler = curl_init();

        if (!is_null($this->user)) {
            curl_setopt($handler, CURLOPT_USERPWD, $this->user . ':' . $this->password);
        }

        Logger::get()->debug('HTTP: method is ' . $method);
        switch ($method) {
            case self::METHOD_DELETE:
                curl_setopt($handler, CURLOPT_URL, $url . '?' . http_build_query($parameters));
                curl_setopt($handler, CURLOPT_CUSTOMREQUEST, self::DELETE);
                break;
            case self::METHOD_POST:
                curl_setopt($handler, CURLOPT_URL, $url);
                curl_setopt($handler, CURLOPT_POST, true);
                curl_setopt($handler, CURLOPT_POSTFIELDS, $parameters);
                break;
            case self::METHOD_GET:
                curl_setopt($handler, CURLOPT_URL, $url . '?' . http_build_query($parameters));
                break;
        }

        curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handler, CURLOPT_HTTPHEADER, $headers);
        Logger::get()->debug('HTTP: headers set.');

        // Send the request to the server.
        Logger::get()->debug("HTTP: requesting $url...");
        $output = curl_exec($handler);
        $errNo = curl_errno($handler);
        $error = curl_error($handler);
        // Obtain the status.
        Logger::get()->debug('HTTP: Done. Getting status...');
        $status = curl_getinfo($handler, CURLINFO_HTTP_CODE);
        // The handler is no longer needed.
        curl_close($handler);
        Logger::get()->debug('HTTP: request finished.');

        if ($errNo) {
            throw new Exception('HTTP: cURL failed: ' . $error, $errNo);
        }

        // If cURL had no errors itself, then there is an HTTP response.
        switch ($status) {
            case self::STATUS_OK:
            case self::STATUS_CREATED:
            case self::STATUS_ACCEPTED:
            case self::STATUS_NO_CONTENT:
                return $output;
            default:
                throw new Http\Exception("HTTP {$status}", $status);
        }
    }
215610
protected function send($method, $endpoint = '', array $headers = null, $body = null)
    {
        $this->lastResponse = null;
        $url = $this->baseUrl . $endpoint;
        $options = ['headers' => \array_merge($this->defaultHeaders, $headers)];

        if (!empty($body)) {
            $options['body'] = $body;
        }

        $this->lastRequest = $this->httpClient->request(
            $method,
            $url,
            $options
        );

        try {
            $this->lastResponse = $this->httpClient->send($this->lastRequest);
        } catch (\Exception $error) {
            throw new HttpServiceException(
                HttpServiceException::BAD_SERVICE_REQUEST,
                [$error->getMessage()]
            );
        }

        return $this->lastResponse;
    }
116004
public function request(string $method, string $url, array $headers = [], string $body = ''): ResponseInterface
    {
        $request = Factory::request($method, $url, $headers, $body);

        return $this->sendRequest($request);
    }