src/SendsHttpRequestTrait.php
public function send(string $method, Client $httpClient, array $path = []): DorcasResponse
    {
        $this->prefillHeader();
        $this->prefillBody();
        if (strtolower($method) !== 'get') {
            # we don't validate GEt requests
            $this->validate();
        }
        $uri = static::getRequestUrl($path);
        $url = $uri->getScheme() . '://' . $uri->getAuthority() . $uri->getPath();
        # set the URL
        try {
            $options = [];
            # the request data
            if (!empty($this->headers)) {
                $options[RequestOptions::HEADERS] = $this->headers;
            }
            if (!empty($uri->getQuery())) {
                # some query parameters are present in the URL
                $options[RequestOptions::QUERY] = parse_query_parameters($uri->getQuery());
            }
            if (strtolower($method) !== 'get') {
                # not a get request
                if (!empty($this->multipart)) {
                    # check if we have some multipart data first
                    foreach ($this->body as $key => $value) {
                        # add the requested body params to the multipart data
                        $this->multipart[] = ['name' => $key, 'contents' => $value];
                    }
                    $options[RequestOptions::MULTIPART] = $this->multipart;

                } elseif (static::isJsonRequest() && !empty($this->body)) {
                    # a JSON request
                    $options[RequestOptions::JSON] = $this->body;

                } elseif (!empty($this->body)) {
                    # we switch to an application/www-form-urlencoded type
                    $options[RequestOptions::FORM_PARAMS] = $this->body;
                }
            }
            $response = $httpClient->request($method, $url, $options);
            return new DorcasResponse((string) $response->getBody());

        } catch (BadResponseException $e) {
            // in the case of a failure, let's know the status
            return new DorcasResponse((string) $e->getResponse()->getBody(), $e->getResponse()->getStatusCode(), $e->getRequest());

        } catch (ConnectException $e) {
            return new DorcasResponse('{"status": "error", "data": "'.$e->getMessage().'"}', 0);
        }
    }
public function send(string $method, Client $httpClient, array $path = []): DorcasResponse
    {
        $this->prefillHeader();
        $this->prefillBody();
        if (strtolower($method) !== 'get') {
            # we don't validate GEt requests
            $this->validate();
        }
        $uri = static::getRequestUrl($path);
        $url = $uri->getScheme() . '://' . $uri->getAuthority() . $uri->getPath();
        # set the URL
        try {
            $options = [];
            # the request data
            if (!empty($this->headers)) {
                $options[RequestOptions::HEADERS] = $this->headers;
            }
            if (!empty($uri->getQuery())) {
                # some query parameters are present in the URL
                $options[RequestOptions::QUERY] = parse_query_parameters($uri->getQuery());
            }
            if (strtolower($method) !== 'get') {
                # not a get request
                if (!empty($this->multipart)) {
                    # check if we have some multipart data first
                    foreach ($this->body as $key => $value) {
                        # add the requested body params to the multipart data
                        $this->multipart[] = ['name' => $key, 'contents' => $value];
                    }
                    $options[RequestOptions::MULTIPART] = $this->multipart;

                } elseif (static::isJsonRequest() && !empty($this->body)) {
                    # a JSON request
                    $options[RequestOptions::JSON] = $this->body;

                } elseif (!empty($this->body)) {
                    # we switch to an application/www-form-urlencoded type
                    $options[RequestOptions::FORM_PARAMS] = $this->body;
                }
            }
            $response = $httpClient->request($method, $url, $options);
            return new DorcasResponse((string) $response->getBody());

        } catch (BadResponseException $e) {
            // in the case of a failure, let's know the status
            return new DorcasResponse((string) $e->getResponse()->getBody(), $e->getResponse()->getStatusCode(), $e->getRequest());

        } catch (ConnectException $e) {
            return new DorcasResponse('{"status": "error", "data": "'.$e->getMessage().'"}', 0);
        }
    }
public function send ( string $ method , Client $ httpClient , array $ path = [ ] ) : DorcasResponse { $ this -> prefillHeader ( ) ; $ this -> prefillBody ( ) ; if ( strtolower ( $ method ) !== 'get' ) { # we don't validate GEt requests $ this -> validate ( ) ; } $ uri = static :: getRequestUrl ( $ path ) ; $ url = $ uri -> getScheme ( ) . '://' . $ uri -> getAuthority ( ) . $ uri -> getPath ( ) ; # set the URL try { $ options = [ ] ; # the request data if ( ! empty ( $ this -> headers ) ) { $ options [ RequestOptions :: HEADERS ] = $ this -> headers ; } if ( ! empty ( $ uri -> getQuery ( ) ) ) { # some query parameters are present in the URL $ options [ RequestOptions :: QUERY ] = parse_query_parameters ( $ uri -> getQuery ( ) ) ; } if ( strtolower ( $ method ) !== 'get' ) { # not a get request if ( ! empty ( $ this -> multipart ) ) { # check if we have some multipart data first foreach ( $ this -> body as $ key => $ value ) { # add the requested body params to the multipart data $ this -> multipart [ ] = [ 'name' => $ key , 'contents' => $ value ] ; } $ options [ RequestOptions :: MULTIPART ] = $ this -> multipart ; } elseif ( static :: isJsonRequest ( ) && ! empty ( $ this -> body ) ) { # a JSON request $ options [ RequestOptions :: JSON ] = $ this -> body ; } elseif ( ! empty ( $ this -> body ) ) { # we switch to an application/www-form-urlencoded type $ options [ RequestOptions :: FORM_PARAMS ] = $ this -> body ; } } $ response = $ httpClient -> request ( $ method , $ url , $ options ) ; return new DorcasResponse ( ( string ) $ response -> getBody ( ) ) ; } catch ( BadResponseException $ e ) { // in the case of a failure, let's know the status return new DorcasResponse ( ( string ) $ e -> getResponse ( ) -> getBody ( ) , $ e -> getResponse ( ) -> getStatusCode ( ) , $ e -> getRequest ( ) ) ; } catch ( ConnectException $ e ) { return new DorcasResponse ( '{"status": "error", "data": "' . $ e -> getMessage ( ) . '"}' , 0 ) ; } }
Sends a HTTP request.

@param string $method
@param Client $httpClient
@param array  $path additional components for the path; e.g.: [$id, 'prices']

@return DorcasResponse
@throws \GuzzleHttp\Exception\GuzzleException
Sends a HTTP request .