bradleyhodges/api-manager

HTTP
in package

Class HTTP Provides an advanced HTTP client with Happy Eyeballs, DNS caching, and HTTP/1, 2, and 3 support.

Table of Contents

Properties

$cookies  : mixed
An object which may contain cookies from the last request.
$lastUri  : string
The last URI that was requested.

Methods

__construct()  : mixed
Constructor to initialize the HTTP class with configurations.
addQueryParameters()  : void
Add global query parameters that will be used on all requests.
cookieJar()  : CookieJar
Returns the cookie jar for managing cookies.
delete()  : ResponseInterface
Send an HTTP DELETE request.
get()  : ResponseInterface
Send an HTTP GET request.
getClient()  : Client
Get the Guzzle HTTP client instance.
impersonateBrowser()  : ResponseInterface
Impersonate a browser and send an HTTP request with browser-like headers.
patch()  : ResponseInterface
Send an HTTP PATCH request.
post()  : ResponseInterface
Send an HTTP POST request.
put()  : ResponseInterface
Send an HTTP PUT request.
request()  : ResponseInterface
Send an HTTP request with the configured client.
requestAsync()  : PromiseInterface
The asynchronous version of the requestBoring method. This returns a promise and is intended to be run in an event loop. If no loop is provided, a new one is created.
resolveHost()  : array<string|int, mixed>
Resolves both IPv4 and IPv6 addresses for the given host using either the system DNS resolver or Cloudflare's DNS-over-HTTPS service, depending on configuration.
setBasicAuth()  : void
Set Basic Authentication credentials with secure password handling.
setBearerToken()  : void
Set Bearer token for authorization.
setDefaultHeaders()  : void
Set default headers to be used on all requests.
useCloudflareDns()  : $this
Enable or disable the use of Cloudflare DNS.
useCookieJar()  : $this
Enable or disable the use of the cookie jar for managing cookies.
useDefaultConfig()  : $this
Enable or disable the use of the default configuration.
useDnsCache()  : $this
Enable or disable DNS caching middleware.
useEvaluateRequestIntention()  : self
Enable or disable evaluation of request intention.
useHappyEyeballs()  : $this
Enable or disable the Happy Eyeballs functionality.
useHttpVersionFallback()  : $this
Enable or disable HTTP version fallback middleware.
useMinTlsVersion()  : $this
Enable or disable the minimum TLS version enforcement.
useRetry()  : $this
Enable or disable the retry middleware.

Properties

$cookies

An object which may contain cookies from the last request.

public mixed $cookies

$lastUri

The last URI that was requested.

public string $lastUri = ''

Methods

__construct()

Constructor to initialize the HTTP class with configurations.

public __construct([array<string|int, mixed> $config = [] ][, CacheInterface|null $dnsCache = null ]) : mixed
Parameters
$config : array<string|int, mixed> = []

Configuration options such as 'base_uri', 'headers', etc.

$dnsCache : CacheInterface|null = null

DNS cache interface, defaults to a filesystem cache if null.

Tags
throws
InvalidArgumentException

If configuration is invalid.

example

$http = new HTTP(['base_uri' => 'https://example.com'], $dnsCache, $errorLogger);

addQueryParameters()

Add global query parameters that will be used on all requests.

public addQueryParameters(array<string|int, mixed> $params) : void
Parameters
$params : array<string|int, mixed>

Key-value pairs of query parameters.

cookieJar()

Returns the cookie jar for managing cookies.

public cookieJar() : CookieJar
Return values
CookieJar

The CookieJar instance.

delete()

Send an HTTP DELETE request.

public delete(string $uri[, array<string, string> $headers = [] ][, array<string, mixed> $queryParams = [] ][, array<string, mixed> $options = [] ][, mixed $catchBody = null ]) : ResponseInterface

This method sends a DELETE request to the specified URI with optional headers, query parameters, and additional options. It detects if body content has been erroneously provided and logs a warning if so. The body content is discarded.

Parameters
$uri : string

The endpoint URI.

$headers : array<string, string> = []

Additional request headers (default: empty).

$queryParams : array<string, mixed> = []

Additional query parameters (default: empty).

$options : array<string, mixed> = []

Additional request options that override defaults (default: empty).

$catchBody : mixed = null
Tags
throws
GuzzleException

If the request fails.

Return values
ResponseInterface

The HTTP response.

get()

Send an HTTP GET request.

public get(string $uri[, array<string, string> $headers = [] ][, array<string, mixed> $queryParams = [] ][, array<string, mixed> $options = [] ][, mixed $catchBody = null ]) : ResponseInterface

This method sends a GET request to the specified URI with optional headers, query parameters, and additional options. It detects if body content has been erroneously provided and logs a warning if so. The body content is discarded.

Parameters
$uri : string

The endpoint URI.

$headers : array<string, string> = []

Additional request headers (default: empty).

$queryParams : array<string, mixed> = []

Additional query parameters (default: empty).

$options : array<string, mixed> = []

Additional request options that override defaults (default: empty).

$catchBody : mixed = null
Tags
throws
GuzzleException

If the request fails.

Return values
ResponseInterface

The HTTP response.

getClient()

Get the Guzzle HTTP client instance.

public getClient() : Client
Return values
Client

The Guzzle client instance.

impersonateBrowser()

Impersonate a browser and send an HTTP request with browser-like headers.

public impersonateBrowser(string $method, string $uri[, mixed $body = null ][, array<string, string> $headers = [] ][, array<string, mixed> $queryParams = [] ][, array<string, mixed> $options = [] ]) : ResponseInterface

This method sends an HTTP request to the specified URI, mimicking the behavior of Chrome on a Windows PC. It sets headers and options to closely resemble what a browser would send, including user-agent, accept, and language headers. It handles cookies, sessions, and redirects similar to a browser.

Supported methods include GET, POST, PUT, PATCH, and DELETE.

Parameters
$method : string

The HTTP method to use (e.g., 'GET', 'POST', 'PUT', 'PATCH', 'DELETE').

$uri : string

The endpoint URI.

$body : mixed = null

The request body, applicable for methods like POST, PUT, and PATCH.

$headers : array<string, string> = []

Additional request headers (default: empty).

$queryParams : array<string, mixed> = []

Additional query parameters (default: empty).

$options : array<string, mixed> = []

Additional request options that override defaults (default: empty).

Tags
throws
GuzzleException

If the request fails.

throws
InvalidArgumentException

If an unsupported HTTP method is provided.

example

$response = $this->impersonateBrowser('GET', 'https://example.com');

Return values
ResponseInterface

The HTTP response.

patch()

Send an HTTP PATCH request.

public patch(string $uri[, mixed $body = null ][, array<string, string> $headers = [] ][, array<string, mixed> $queryParams = [] ][, array<string, mixed> $options = [] ]) : ResponseInterface

This method sends a PATCH request to the specified URI with optional body data, headers, query parameters, and additional options. It automatically detects the content type based on the body and formats the request accordingly. An idempotency key is automatically added to ensure idempotency for PATCH requests.

Parameters
$uri : string

The endpoint URI.

$body : mixed = null

The request body, which could be JSON, form data, or other supported types.

$headers : array<string, string> = []

Additional request headers (default: empty).

$queryParams : array<string, mixed> = []

Additional query parameters (default: empty).

$options : array<string, mixed> = []

Additional request options that override defaults (default: empty).

Tags
throws
GuzzleException

If the request fails.

throws
InvalidArgumentException

If body formatting fails or an unsupported content type is provided.

Return values
ResponseInterface

The HTTP response.

post()

Send an HTTP POST request.

public post(string $uri[, mixed $body = null ][, array<string, string> $headers = [] ][, array<string, mixed> $queryParams = [] ][, array<string, mixed> $options = [] ]) : ResponseInterface

This method sends a POST request to the specified URI with optional body data, headers, query parameters, and additional options. It automatically detects the content type based on the body and formats the request accordingly.

Parameters
$uri : string

The endpoint URI.

$body : mixed = null

The request body, which could be JSON, form data, or other supported types.

$headers : array<string, string> = []

Additional request headers (default: empty).

$queryParams : array<string, mixed> = []

Additional query parameters (default: empty).

$options : array<string, mixed> = []

Additional request options that override defaults (default: empty).

Tags
throws
GuzzleException

If the request fails.

throws
InvalidArgumentException

If body formatting fails or an unsupported content type is provided.

Return values
ResponseInterface

The HTTP response.

put()

Send an HTTP PUT request.

public put(string $uri[, mixed $body = null ][, array<string, string> $headers = [] ][, array<string, mixed> $queryParams = [] ][, array<string, mixed> $options = [] ]) : ResponseInterface

This method sends a PUT request to the specified URI with optional body data, headers, query parameters, and additional options. It automatically detects the content type based on the body and formats the request accordingly. An idempotency key is automatically added to ensure idempotency for PUT requests.

Parameters
$uri : string

The endpoint URI.

$body : mixed = null

The request body, which could be JSON, form data, or other supported types.

$headers : array<string, string> = []

Additional request headers (default: empty).

$queryParams : array<string, mixed> = []

Additional query parameters (default: empty).

$options : array<string, mixed> = []

Additional request options that override defaults (default: empty).

Tags
throws
GuzzleException

If the request fails.

throws
InvalidArgumentException

If body formatting fails or an unsupported content type is provided.

Return values
ResponseInterface

The HTTP response.

request()

Send an HTTP request with the configured client.

public request(string $method, string $uri[, array<string|int, mixed> $options = [] ]) : ResponseInterface

This method supports the Happy Eyeballs algorithm for resolving hostnames if enabled. It merges the default headers with any request-specific headers, adds an idempotency key for POST requests, and logs the request and response details.

The method blocks until the asynchronous operation completes, making it appear synchronous to the caller.

Parameters
$method : string

HTTP method (e.g., 'GET', 'POST'). Must be uppercase and conform to RFC 7231.

$uri : string

Endpoint URI, which can be relative to the base URI. Must be validated to avoid SSRF attacks.

$options : array<string|int, mixed> = []

Request options compatible with Guzzle, such as 'headers', 'query', etc.

Tags
throws
GuzzleException

If the request fails.

throws
RuntimeException

If both IPv6 and IPv4 connection attempts fail, or other unexpected issues occur.

throws
InvalidArgumentException

If input parameters are invalid.

Return values
ResponseInterface

The HTTP response.

requestAsync()

The asynchronous version of the requestBoring method. This returns a promise and is intended to be run in an event loop. If no loop is provided, a new one is created.

public requestAsync(string $method, string $uri[, array<string|int, mixed> $options = [] ][, LoopInterface|null $loop = null ]) : PromiseInterface
Parameters
$method : string

HTTP method (e.g., 'GET', 'POST').

$uri : string

Endpoint URI, can be relative to the base URI.

$options : array<string|int, mixed> = []

Request options compatible with Guzzle, such as 'headers', 'query', etc.

$loop : LoopInterface|null = null

The ReactPHP event loop. If null, a new loop will be created.

Return values
PromiseInterface

A promise that resolves to the HTTP response.

resolveHost()

Resolves both IPv4 and IPv6 addresses for the given host using either the system DNS resolver or Cloudflare's DNS-over-HTTPS service, depending on configuration.

public resolveHost(string $host) : array<string|int, mixed>
Parameters
$host : string

The hostname to resolve.

Tags
throws
InvalidArgumentException

If the host is invalid.

Return values
array<string|int, mixed>

An array with 'ipv4' and 'ipv6' keys containing the resolved addresses, or null if resolution fails.

setBasicAuth()

Set Basic Authentication credentials with secure password handling.

public setBasicAuth(string $username, string $password) : void
Parameters
$username : string

The username.

$password : string

The password.

setBearerToken()

Set Bearer token for authorization.

public setBearerToken(string $token) : void
Parameters
$token : string

The bearer token.

setDefaultHeaders()

Set default headers to be used on all requests.

public setDefaultHeaders(array<string|int, mixed> $headers) : void
Parameters
$headers : array<string|int, mixed>

Key-value pairs of headers.

useCloudflareDns()

Enable or disable the use of Cloudflare DNS.

public useCloudflareDns(bool $enabled) : $this
Parameters
$enabled : bool

Whether to use Cloudflare DNS.

Return values
$this

useCookieJar()

Enable or disable the use of the cookie jar for managing cookies.

public useCookieJar(bool $enabled) : $this
Parameters
$enabled : bool

Whether to use the cookie jar.

Return values
$this

useDefaultConfig()

Enable or disable the use of the default configuration.

public useDefaultConfig(bool $enabled) : $this
Parameters
$enabled : bool

Whether to use the default configuration.

Return values
$this

useDnsCache()

Enable or disable DNS caching middleware.

public useDnsCache(bool $enabled) : $this
Parameters
$enabled : bool

Whether to enable DNS caching middleware.

Return values
$this

useEvaluateRequestIntention()

Enable or disable evaluation of request intention.

public useEvaluateRequestIntention(bool $evaluate) : self
Parameters
$evaluate : bool

Whether to evaluate request intentions.

Return values
self

useHappyEyeballs()

Enable or disable the Happy Eyeballs functionality.

public useHappyEyeballs(bool $enabled) : $this
Parameters
$enabled : bool

Whether to enable Happy Eyeballs.

Return values
$this

useHttpVersionFallback()

Enable or disable HTTP version fallback middleware.

public useHttpVersionFallback(bool $enabled) : $this
Parameters
$enabled : bool

Whether to enable HTTP version fallback middleware.

Return values
$this

useMinTlsVersion()

Enable or disable the minimum TLS version enforcement.

public useMinTlsVersion(bool $enabled) : $this
Parameters
$enabled : bool

Whether to enforce minimum TLS version.

Return values
$this

useRetry()

Enable or disable the retry middleware.

public useRetry(bool $enabled) : $this
Parameters
$enabled : bool

Whether to enable retry middleware.

Return values
$this

        
On this page

Search results