Installation
composer require chaseisabelle/phprom-client
Ensure your project uses PHP 7.4+ (last release compatibility).
First Use Case: Query Execution
use ChaseIsabelle\PhpromClient\Client;
$client = new Client('http://localhost:9200'); // Default Phprom endpoint
$response = $client->search('index_name', ['match_all' => new \stdClass()]);
Key Files to Explore
src/Client.php – Core HTTP client logic.src/RequestBuilder.php – Query construction helpers.src/Exceptions/ – Error handling patterns.Indexing Documents
$client->index('users', 1, ['name' => 'John']);
Searching with DSL
$query = [
'query' => [
'bool' => [
'must' => [
['match' => ['name' => 'John']],
['range' => ['age' => ['gte' => 30]]]
]
]
]
];
$results = $client->search('users', $query);
Bulk Operations
$bulkData = [
['index' => ['_index' => 'users', '_id' => 1]],
['name' => 'Alice'],
['index' => ['_index' => 'users', '_id' => 2]],
['name' => 'Bob']
];
$client->bulk($bulkData);
Laravel Service Provider
Bind the client in AppServiceProvider for dependency injection:
$this->app->singleton(Client::class, fn() => new Client(config('phprom.url')));
Query Builder Abstraction
Extend RequestBuilder to add domain-specific methods:
class UserQueryBuilder extends RequestBuilder {
public function activeUsers() {
return $this->addQuery([
'bool' => ['must_not' => ['term' => ['active' => false]]]
]);
}
}
Async Requests Use Guzzle’s async capabilities (if underlying HTTP client supports it):
$promise = $client->getHttpClient()->requestAsync('GET', '/users/_search');
Deprecated Endpoints
The package predates Phprom’s API changes (e.g., _msearch vs. /_search). Verify endpoints against Phprom’s docs.
Error Handling
Exceptions are thrown for HTTP errors (4xx/5xx), but responses may still contain useful data. Check $response->getBody() for details:
try {
$client->search('nonexistent', []);
} catch (\ChaseIsabelle\PhpromClient\Exception\ClientException $e) {
$error = json_decode($e->getResponse()->getBody(), true);
}
Serialization Quirks
The library uses json_encode/json_decode by default. For custom objects, implement JsonSerializable or configure a custom encoder:
$client->setEncoder(new CustomJsonEncoder());
Enable Debug Mode
$client->setDebug(true); // Logs raw requests/responses
Mocking for Tests
Use a test HTTP client (e.g., Guzzle’s MockHandler):
$mock = new MockHandler([new Response(200, [], '{"hits": []}')]);
$client->setHttpClient(new Client($mock));
Custom HTTP Client
Replace the default client (Guzzle) by implementing ChaseIsabelle\PhpromClient\Contracts\HttpClient:
$client->setHttpClient(new CustomHttpClient());
Middleware
Add request/response middleware via the setMiddleware method:
$client->setMiddleware(function ($request) {
$request->setHeader('X-Custom-Header', 'value');
});
Response Transformation
Override transformResponse in a custom client class to parse responses differently:
class CustomClient extends Client {
protected function transformResponse($response) {
return parent::transformResponse($response)->withMetadata(['custom' => true]);
}
}
How can I help you explore Laravel packages today?