HttpClient
in package
Simple and versatile static HTTP client based on cURL
This class provides a simplified static interface for making HTTP requests using PHP's cURL extension. It supports all main HTTP methods, concurrent multiple requests, automatic JSON handling, and flexible configuration options.
Main features:
- Support for GET, POST, PUT, DELETE, PATCH, HEAD
- Asynchronous multiple requests with curl_multi
- Automatic JSON response parsing
- Configurable timeout, redirect, and SSL handling
- Customizable headers and default options
- Comprehensive error handling
- Fully static interface
Tags
Table of Contents
Methods
- delete() : array<string|int, mixed>|false
- Performs an HTTP DELETE request
- execute_multi() : array<string|int, mixed>|false
- Executes multiple HTTP requests in parallel
- get() : array<string|int, mixed>|false
- Performs an HTTP GET request
- get_default_options() : array<string|int, mixed>
- Returns the current default options
- get_last_error() : string
- Returns the last occurred error
- head() : array<string|int, mixed>|false
- Performs an HTTP HEAD request
- patch() : array<string|int, mixed>|false
- Performs an HTTP PATCH request
- post() : array<string|int, mixed>|false
- Performs an HTTP POST request
- put() : array<string|int, mixed>|false
- Performs an HTTP PUT request
- request() : array<string|int, mixed>|false
- Main method for performing HTTP requests
- reset_default_options() : void
- Resets all options to their original default values
- set_default_option() : void
- Sets a single default option
- set_default_options() : void
- Sets multiple default options
Methods
delete()
Performs an HTTP DELETE request
public
static delete(string $url[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|false
Parameters
- $url : string
-
Target URL
- $options : array<string|int, mixed> = []
-
Additional request options
Return values
array<string|int, mixed>|false —Response array or false on error
execute_multi()
Executes multiple HTTP requests in parallel
public
static execute_multi(array<string|int, mixed> $requests) : array<string|int, mixed>|false
Allows executing multiple HTTP requests simultaneously to improve performance when data needs to be retrieved from multiple endpoints.
Parameters
- $requests : array<string|int, mixed>
-
Associative array of requests in the format: ['key' => ['url' => 'https://...', 'options' => [...]]]
Tags
Return values
array<string|int, mixed>|false —Associative array with responses (key => response) or false on error
get()
Performs an HTTP GET request
public
static get(string $url[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|false
Parameters
- $url : string
-
Target URL
- $options : array<string|int, mixed> = []
-
Additional request options
Tags
Return values
array<string|int, mixed>|false —Response array or false on error
get_default_options()
Returns the current default options
public
static get_default_options() : array<string|int, mixed>
Return values
array<string|int, mixed> —Array of default options
get_last_error()
Returns the last occurred error
public
static get_last_error() : string
Return values
string —Error message or empty string if no error
head()
Performs an HTTP HEAD request
public
static head(string $url[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|false
Useful to get only response headers without the response body
Parameters
- $url : string
-
Target URL
- $options : array<string|int, mixed> = []
-
Additional request options
Return values
array<string|int, mixed>|false —Response array or false on error
patch()
Performs an HTTP PATCH request
public
static patch(string $url[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|false
Parameters
- $url : string
-
Target URL
- $options : array<string|int, mixed> = []
-
Additional request options
Return values
array<string|int, mixed>|false —Response array or false on error
post()
Performs an HTTP POST request
public
static post(string $url[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|false
Parameters
- $url : string
-
Target URL
- $options : array<string|int, mixed> = []
-
Additional request options
Tags
Return values
array<string|int, mixed>|false —Response array or false on error
put()
Performs an HTTP PUT request
public
static put(string $url[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|false
Parameters
- $url : string
-
Target URL
- $options : array<string|int, mixed> = []
-
Additional request options
Return values
array<string|int, mixed>|false —Response array or false on error
request()
Main method for performing HTTP requests
public
static request(string $url[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|false
This method centralizes the logic for all HTTP requests, handling cURL configuration, execution, and response parsing.
Parameters
- $url : string
-
Target URL
- $options : array<string|int, mixed> = []
-
Request options that override default ones
Return values
array<string|int, mixed>|false —Associative array with the response or false on error
The returned response has the following structure:
- status_code: HTTP status code
- headers: Associative array of response headers
- body: Response body (automatically decoded if JSON)
- info: Additional information from curl_getinfo()
reset_default_options()
Resets all options to their original default values
public
static reset_default_options() : void
set_default_option()
Sets a single default option
public
static set_default_option(string $key, mixed $value) : void
Parameters
- $key : string
-
Option key
- $value : mixed
-
Option value
Tags
set_default_options()
Sets multiple default options
public
static set_default_options(array<string|int, mixed> $options) : void
Parameters
- $options : array<string|int, mixed>
-
Associative array of options