Requests

The HttpClientRequest interface defines all methods useful for a client request. You can access the request object only for prepared requests.

$request = $client->prepareRequest('POST', 'https://example.com/api/foo');

getMethod

getMethod(): string

With this method, you can get the method of the request (i.e, GET, POST, etc).

$request->getMethod();  // POST, GET, PATCH, ...

getUri

getUri(): string

With this method, you can get the URI of the request.

$request->getUri();  // e.g. https://example.com/api/foo

getHeader

getHeader(key: string): string

With this method, you can get a particular header.

$request->getHeader('Content-Type');  // e.g. application/json

getHeaders

getHeader(): array

With this method, you can get all defined headers for the request.

$request->getHeaders();

All these headers are key-value pairs like the following.

['Content-Type' => 'application/json;charset=UTF-8', 'Accept' => 'application/json']

getJson

getJson(): array

With this method, you can get the JSON body defined for the request.

$request->getJson();

This JSON body is represented as an array of key-value pairs like the following.

['foo' => 'bar', 'flag' => 'enabled']

getQuery

getQuery(): array

With this method, you can get the query defined for the request.

$request->getQuery();

This query is represented as an array of key-value pairs like the following.

['foo' => 'bar', 'flag' => 'enabled']

getTimeout

getTimeout(): int

With this method, you can get the connection timeout (in seconds) defined for the request.

$request->getQuery();   // e.g 10

getSecurityContext

getSecurityContext(): HttpSecurityContext

With this method, you can get the security context. This class represents the object containing the certificates to trust when making a secure client connection, and the certificate chain and private key to serve from a secure server.

getBasicAuth

getBasicAuth(): array

With this method, you can get an array of username and password for basic authentication. The first value will be the username and the second one will be the password.

['username', 'password']

hasJson

hasJson(): bool

With this method, you can verify if a JSON body was defined for the request.

$request->hasJson();   // true, false

hasQuery

hasQuery(): bool

With this method, you can verify if query parameters were defined for the request.

$request->hasQuery();   // true, false

hasHeaders

hasHeaders(): bool

With this method, you can verify if headers were defined for the request.

$request->hasHeaders();   // true, false

hasSecurityContext

hasSecurityContext(): bool

With this method, you can verify if a security context was defined for the request.

$request->hasSecurityContext();   // true, false

hasBasicAuth

hasBasicAuth(): bool

With this method, you can verify if a basic auth was defined for the request.

$request->hasBasicAuth();   // true, false

setMethod

setMethod(method: string): self

With this method, you can set the method of the request (e.g, GET, POST, ...).

$request->setMethod('POST');

setUri

setUri(uri: string): self

With this method, you can set the URI of the request.

$request->setUri('https://example.com/api/foo');

setHeader

setHeader(key: string, value: string): self

With this method, you can set a header for the request. As the header is a key-value pair you can define the key using the first function parameter and the value using the second one.

setHeader('Content-Type', 'application/json;charset=UTF-8')

setHeaders

setHeaders(headers: array): self

With this method, you can define a set of headers for the request. The input value must be a valid array of key-value pairs like the following.

setHeaders([
   'Content-Type' =>'application/json;charset=UTF-8',
   'Accept' => 'application/json'
])

setJson

setJson(json: array): self

With this method, you can define a JSON body for the request. The input value must be a valid array of key-value pairs like the following.

setJson([
   'foo' => 'bar',
   'flag' => 'enabled'
])

setQuery

setQuery(query: array): self

With this method, you can define query parameters for the request. The input value must be a valid array of key-value pairs like the following.

setQuery([
   'foo' => 'bar',
   'flag' => 'enabled'
])

setTimeout

setTimeout(timeout: int): self

With this method, you can define the connection timeout (in seconds) for the request.

$request->setTimeout(10);

setSecurityContext

setSecurityContext(httpSecurityContext: HttpSecurityContext): self

With this method, you can define a security context for the request.

setBasicAuth

setBasicAuth(username: string, password: string): self

With this method, you can set the username and password for basic authentication.

$request->setBasicAuth('username', 'password');

ssl

ssl(ssl: bool): self

With this method, you can set SSL verification.

$request->ssl(true);    // enable SSL verification
$request->ssl(false);    // disable SSL verification

isSSL

isSSL(): bool

With this method, you can verify if SSL verification is enabled.

$request->isSSL();   // true, false