A simple, intuitive PHP HTTP client that provides a clean wrapper around popular HTTP libraries like Guzzle and rmccue/requests. Make HTTP requests with minimal code and maximum flexibility.
- Simple API: Clean, one-line HTTP requests without complex configuration
- Multiple Backends: Automatic driver selection (Guzzle preferred, rmccue/requests fallback)
- Parallel Requests: Built-in support for concurrent HTTP requests using curl_multi_*
- Flexible Response Handling: Access response data via methods, properties, or array syntax
- JSON Support: Built-in JSON parsing with JBZoo/Data integration
- Event System: Hook into request lifecycle with event listeners
- PHP 8.2+ Ready: Modern PHP with strict typing and best practices
- PHP 8.2 or higher
- ext-json
composer require guzzlehttp/guzzle --no-update # Recommended, but not required
composer require jbzoo/http-client
use JBZoo\HttpClient\HttpClient;
// Simple GET request
$client = new HttpClient();
$response = $client->request('https://api.github.com/users/octocat');
echo $response->getBody(); // JSON response
echo $response->getCode(); // 200
use JBZoo\HttpClient\HttpClient;
// Configure client (no options required!)
$httpClient = new HttpClient([
'auth' => [ // Simple HTTP auth
'http-user-name',
'http-password'
],
'headers' => [ // Your custom headers
'X-Custom-Header' => 42,
],
'driver' => 'auto', // (Auto|Guzzle5|Guzzle6|Rmccue)
'timeout' => 10, // Wait in seconds
'verify' => false, // Check cert for SSL
'exceptions' => false, // Show exceptions for statuses 4xx and 5xx
'allow_redirects' => true, // Show real 3xx-header or result?
'max_redirects' => 10, // How much to redirect?
'user_agent' => "It's me", // Custom UserAgent
]);
// Just request
$response = $httpClient->request('http://my.site.com/', [
'key-1' => 'value-1',
'key-2' => 'value-2'
], 'post');
// Get status code
$code = $response->getCode();
$code = $response->code;
$code = $response['code'];
// Get headers
$headers = $response->getHeaders();
$headers = $response->headers;
$headers = $response['headers'];
$header = $response->getHeader('X-Custom-Header-Response');
$header = $response->find('headers.x-custom-header-response', 'default-value', 'trim');
// Get response body
$body = $response->getBody();
$body = $response->body;
$body = $response['body'];
// Parse JSON response (uses JBZoo/Data)
$json = $response->getJSON();
$value = $json->get('key', 'default', 'trim');
$value = $json->find('key.nested', 'default', 'trim');
use JBZoo\HttpClient\HttpClient;
$httpClient = new HttpClient();
$results = $httpClient->multiRequest(array(
'request_0' => 'http://mockbin.org/request',
'request_1' => ['http://mockbin.org/request', [
'args' => ['key' => 'value']
]],
'request_2' => ['http://mockbin.org/request', [
'method' => 'post',
'args' => ['key' => 'value'],
'headers' => [
'X-Custom-Header' => 42,
],
'timeout' => 10,
'verify' => false,
'exceptions' => false,
'allow_redirects' => true,
'max_redirects' => 10,
'user_agent' => 'JBZoo/Http-Client v1.x-dev'
]]
]);
$results['request_0']->getBody();
$results['request_1']->getBody();
$results['request_2']->getBody();
make update # Install/update dependencies
make test-all # Run tests and code style checks
make test # Run PHPUnit tests only
make codestyle # Run code style checks only
For testing purposes, you can start a mock HTTP server:
make start-mock-server # Starts httpbin on port 8087
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Run tests (
make test-all
) - Commit your changes (
git commit -am 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
MIT - see LICENSE file for details.