MilkCore

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
example
// Basic usage example

// Simple GET request
$response = HttpClient::get('https://api.example.com/users');
if ($response && $response['status_code'] === 200) {
    echo "Users found: " . count($response['body']);
}

// POST request with JSON data
$response = HttpClient::post('https://api.example.com/users', [
    'body' => ['name' => 'John Doe', 'email' => 'john@example.com'],
    'headers' => ['Authorization' => 'Bearer abc123']
]);

// Configure default options
HttpClient::set_default_options([
    'timeout' => 60,
    'headers' => ['Accept' => 'application/json']
]);

// Concurrent multiple requests
$requests = [
    'users' => ['url' => 'https://api.example.com/users'],
    'posts' => ['url' => 'https://api.example.com/posts', 'options' => ['timeout' => 30]],
    'comments' => ['url' => 'https://api.example.com/comments', 'options' => ['headers' => ['X-Key' => 'value']]]
];
$responses = HttpClient::execute_multi($requests);

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
example
$requests = [
    'users' => [
        'url' => 'https://api.example.com/users',
        'options' => ['timeout' => 30]
    ],
    'posts' => [
        'url' => 'https://api.example.com/posts',
        'options' => ['headers' => ['Authorization' => 'Bearer token']]
    ],
    'comments' => [
        'url' => 'https://api.example.com/comments'
        // options is optional
    ]
];

$responses = HttpClient::execute_multi($requests);
foreach ($responses as $key => $response) {
    if (isset($response['error'])) {
        echo "Error for {$key}: " . $response['message'] . "\n";
    } else {
        echo "Success for {$key}: " . $response['status_code'] . "\n";
    }
}

Each response in the array can be:

  • An array with status_code, headers, body, info (on success)
  • An array with error=true and message (on error)
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
example
$response = HttpClient::get('https://api.example.com/data', [
    'headers' => ['Accept' => 'application/json'],
    'timeout' => 60
]);
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
example
$response = HttpClient::post('https://api.example.com/users', [
    'body' => ['name' => 'John', 'email' => 'john@test.com'],
    'headers' => ['Content-Type' => 'application/json']
]);
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
example
HttpClient::set_default_option('timeout', 60);
HttpClient::set_default_option('user_agent', 'MyApp/1.0');

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

Tags
example
HttpClient::set_default_options([
    'timeout' => 60,
    'headers' => ['Accept' => 'application/json'],
    'verify_ssl' => false
]);

        
On this page

Search results