amphp/artax
Deprecated (unmaintained) async HTTP/1.1 client for PHP built on Amp. Implements HTTP over raw TCP sockets (no ext/curl), with keep-alive pooling, redirects, gzip decoding, streaming bodies, TLS, cookies, and proxy support. Use amphp/http-client instead.
Installation
composer require amphp/artax
(Note: This package is deprecated; prefer amphp/http-client for new projects.)
Basic Request
use Amp\Loop;
use Artax\Client;
use Artax\Request;
Loop::run(function () {
$client = new Client();
$request = new Request('https://example.com');
$response = yield $client->request($request);
echo $response->getBody();
});
First Use Case: Parallel Requests
$client = new Client();
$requests = [
new Request('https://api.example.com/users'),
new Request('https://api.example.com/posts'),
];
$responses = yield $client->requestAll($requests);
foreach ($responses as $response) {
echo $response->getBody();
}
3.0.8 for null pointer access, 3.0.12 for timeout leaks).Asynchronous Requests
Use yield with Client::request() to avoid blocking. Ideal for I/O-bound tasks (e.g., scraping, APIs).
$response = yield $client->request(new Request('https://example.com'));
Connection Pooling Artax reuses HTTP/1.1 keep-alive connections by default. Configure timeouts per request:
$request = new Request('https://example.com');
$request->setTimeout(5.0); // 5-second timeout
Streaming Responses Handle large responses without loading them entirely into memory:
$response = yield $client->request(new Request('https://example.com/large-file'));
$body = yield $response->getBody();
while (!$body->isFinished()) {
$chunk = yield $body->read();
// Process chunk
}
Redirect Handling
Enable/disable redirects via Request::setFollowRedirects() (default: true).
$request->setFollowRedirects(false); // Disable for custom logic
Form Submissions
Use Request::setFormParams() for multipart/form-data:
$request->setFormParams(['key' => 'value']);
$request->setMethod('POST');
Laravel Integration: Wrap Artax in a Laravel service provider to expose a singleton client:
// app/Providers/ArtaxServiceProvider.php
public function register()
{
$this->app->singleton(Client::class, function () {
return new Client();
});
}
Use in controllers:
use Amp\Loop;
use Artax\Client;
public function fetchData()
{
$client = app(Client::class);
$response = Loop::run(function () use ($client) {
return $client->request(new Request('https://api.example.com/data'));
});
return $response->getBody();
}
Error Handling:
Catch Artax\Exception\SocketException, Artax\Exception\TimeoutException, etc.:
try {
$response = yield $client->request($request);
} catch (Artax\Exception\ExceptionInterface $e) {
Log::error("Request failed: " . $e->getMessage());
}
Testing:
Use Artax\MockHttpClient for unit tests (if available) or mock the Client interface.
Deprecation Warning:
Artax is no longer maintained. Migrate to amphp/http-client for long-term projects.
Memory Leaks:
3.0.7 fix for GC issues).$body = yield $response->getBody();
yield $body->read(); // Consume fully or stream
Timeouts:
3.0.12).$request->setTimeout(10.0); // Connection + transfer timeout
Redirects with Host Changes:
http → https) may fail silently (fixed in 3.0.0).Request::setFollowRedirects(true) and check logs for host mismatches.Cookie Handling:
3.0.2).amphp/http-client.Chunked Transfers:
Content-Length or Transfer-Encoding: chunked may hang (fixed in 3.0.11).Enable Verbose Logging:
$client = new Client();
$client->setLogger(new \Monolog\Logger('artax', [
new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::DEBUG),
]));
Inspect Headers:
$headers = $response->getHeaders();
print_r($headers->toArray());
Check for Aborted Requests:
yield $response->getBody() to detect incomplete responses (e.g., server disconnects).Custom Middleware:
Intercept requests/responses via Client::addMiddleware():
$client->addMiddleware(function (Request $request, callable $next) {
$request->setHeader('X-Custom-Header', 'value');
return $next($request);
});
Proxy Support:
Configure proxies via Client::setProxy():
$client->setProxy('http://proxy.example.com:8080');
TLS Customization: Override TLS settings (e.g., for self-signed certs):
$client->setTlsContext(function (\GuzzleHttp\Ring\Client\TlsContext $context) {
$context->setCaFile('/path/to/cert.pem');
});
3.0.9) to reduce memory usage.3.0.1) to prevent zip bombs. Adjust via Request::setBodySizeLimit().How can I help you explore Laravel packages today?