elastic/transport
PSR-7/17/18 HTTP transport library for Elastic PHP clients. Uses HTTPlug discovery for clients/factories and supports async via compatible PSR-18 clients (e.g., Guzzle, Symfony). Falls back to a built-in cURL client if none found.
Start by installing the package with Composer:
composer require elastic/transport
If no PSR-18 client is available, elastic/transport will fall back to a built-in cURL-based client (requires ext-curl). For production or async support, explicitly install a PSR-18 client:
composer require guzzlehttp/guzzle
# or
composer require symfony/http-client
Instantiate the transport with minimal config:
use Elastic\Transport\TransportBuilder;
$transport = TransportBuilder::create()
->setHosts(['http://localhost:9200'])
->build();
Send a basic request:
$request = (new Elastic\Transport\Http\Discovery\Psr17FactoryDiscovery::findRequestFactory())
->createRequest('GET', '/_cluster/health');
$response = $transport->sendRequest($request);
Check the last-used node via $transport->lastRequest()->getUri().
setHosts() to distribute load. By default, requests rotate across nodes using a randomized Round-robin selector.$transport->setRetries(2);
Combined with a cluster, this automatically retries on different nodes.NodePoolInterface via setNodePool(), specifying a custom SelectorInterface (e.g., latency-based) and ResurrectInterface.sendAsyncRequest() for non-blocking calls. Handle results with then() callbacks or wait() for blocking resolution — requires a PSR-18 + HttpAsyncClient client (e.g., Guzzle).OTEL_PHP_INSTRUMENTATION_ELASTICSEARCH_ENABLED=true and leverage OTel spans.setClient() to integrate with Symfony HttpClient, Guzzle, or custom implementations — useful in frameworks (e.g., Laravel) where PSR-18 clients are already configured.https://example.com/_cluster/health), the NodePool is bypassed entirely — make sure paths are relative unless you intentionally route elsewhere.setClient() sets both sync and async unless setAsyncClient() is explicitly called. Use this if your async client (e.g., ReactPHP-based) differs from the sync one.NoResurrect strategy never recovers dead nodes. Switch to ElasticsearchResurrect in Elasticsearch contexts: it probes / periodically to revive nodes./_nodes/stats instead of full URLs when building requests — lets the transport handle routing and host selection.INFO or customizing the handler to avoid performance issues.src/Client/Curl.php) lacks async capabilities — avoid it for jobs requiring concurrency; always pair with an async-capable HTTP client for production async patterns.$opts parameter of sendRequest() for richer instrumentation:
$transport->sendRequest($request, [
'elastic.request.method' => 'search',
'custom.trace_id' => $someId
]);
How can I help you explore Laravel packages today?