Milk Admin
HttpClient
Revision: 2025-11-11
Static HTTP client based on cURL with support for all major HTTP methods, concurrent requests, automatic JSON handling, and exception-based error handling.
Usage
use App\HttpClient;
use App\Exceptions\HttpClientException;
// Simple GET request
try {
$response = HttpClient::get('https://api.example.com/users');
echo "Status: {$response['status_code']}";
} catch (HttpClientException $e) {
// Invalid URL or configuration error
echo "Error: " . $e->getMessage();
}
Exception Handling
Available exceptions:
HttpClientException- Invalid URL, empty requests array, JSON encoding errors, cURL errors, connection failures, timeout errors
Methods
get(string $url, array $options = []) : array
Performs an HTTP GET request.
$response = HttpClient::get('https://api.example.com/users', [
'headers' => ['Authorization' => 'Bearer token'],
'timeout' => 60
]);
post(string $url, array $options = []) : array
Performs an HTTP POST request.
$response = HttpClient::post('https://api.example.com/users', [
'body' => ['name' => 'John', 'email' => 'john@example.com']
]);
put(string $url, array $options = []) : array
Performs an HTTP PUT request.
delete(string $url, array $options = []) : array
Performs an HTTP DELETE request.
patch(string $url, array $options = []) : array
Performs an HTTP PATCH request.
head(string $url, array $options = []) : array
Performs an HTTP HEAD request. Returns only headers without body.
executeMulti(array $requests) : array
Executes multiple HTTP requests in parallel.
$requests = [
'users' => ['url' => 'https://api.example.com/users'],
'posts' => [
'url' => 'https://api.example.com/posts',
'options' => ['timeout' => 30]
]
];
try {
$responses = HttpClient::executeMulti($requests);
foreach ($responses as $key => $response) {
if (isset($response['error'])) {
echo "Error for {$key}: {$response['message']}\n";
} else {
echo "Success: {$response['status_code']}\n";
}
}
} catch (HttpClientException $e) {
echo "Multi request failed: " . $e->getMessage();
}
setDefaultOption(string $key, mixed $value) : void
Sets a single default option for all requests.
HttpClient::setDefaultOption('timeout', 60);
setDefaultOptions(array $options) : void
Sets multiple default options at once.
HttpClient::setDefaultOptions([
'timeout' => 60,
'headers' => ['Accept' => 'application/json'],
'user_agent' => 'MyApp/1.0'
]);
getDefaultOptions() : array
Returns current default options.
resetDefaultOptions() : void
Resets all options to framework defaults.
Response Structure
All methods return an associative array:
$response = [
'status_code' => 200,
'headers' => ['Content-Type' => 'application/json'],
'body' => [...], // Auto-decoded if JSON
'info' => [...] // cURL metadata
];
Available Options
timeout- Request timeout in seconds (default: 30)connect_timeout- Connection timeout in seconds (default: 10)follow_redirects- Follow redirects (default: true)max_redirects- Max redirects to follow (default: 5)verify_ssl- Verify SSL certificates (default: true)user_agent- User agent string (default: 'MilkHttpClient/1.0')headers- Custom headers array (default: [])body- Request body (arrays/objects auto-encoded to JSON)
Examples
Complete REST API Client
try {
$response = HttpClient::get('https://www.milkadmin.org/ma32r4c2aa/api.php?page=home/get', ['timeout' => 2]);
if ($response['status_code'] == 200) {
echo $response['body'];
}
} catch (\App\Exceptions\HttpClientException $e) {
// Fallback to local welcome page if HTTP request fails
echo "Error: " . $e->getMessage();
}
Loading...