openseach-php removes the hard-coded dependency on the Guzzle HTTP client and switches to the following PSR interfaces:
You can continue to use Guzzle, but will need to configure it as a PSR-18 HTTP Client.
opensearch-php 2.x will try and discover and install a PSR HTTP Client using PHP-HTTP Discovery if one is not explicitly provided.
$transport = (new \OpenSearch\TransportFactory())->create();
$endpointFactory = new \OpenSearch\EndpointFactory();
$client = new Client($transport, $endpointFactory, []);
// Send a request to the 'info' endpoint.
$info = $client->info();
To configure Guzzle as a PSR HTTP Client with the similar configuration to opensearch 1.x you can use the following example:
$guzzleClient = new \GuzzleHttp\Client([
'base_uri' => 'https://localhost:9200',
'auth' => ['admin', getenv('OPENSEARCH_PASSWORD')],
'verify' => false,
'retries' => 2,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'User-Agent' => sprintf('opensearch-php/%s (%s; PHP %s)', \OpenSearch\Client::VERSION, PHP_OS, PHP_VERSION),
]
]);
$guzzleHttpFactory = new \GuzzleHttp\Psr7\HttpFactory();
$serializer = new \OpenSearch\Serializers\SmartSerializer();
$requestFactory = new \OpenSearch\RequestFactory(
$guzzleHttpFactory,
$guzzleHttpFactory,
$guzzleHttpFactory,
$serializer,
);
$transport = (new OpenSearch\TransportFactory())
->setHttpClient($guzzleClient)
->setRequestFactory($requestFactory)
->create();
$endpointFactory = new \OpenSearch\EndpointFactory();
$client = new \OpenSearch\Client($transport, $endpointFactory, []);
// Send a request to the 'info' endpoint.
$info = $client->info();
You can configure Symfony HTTP Client as a PSR HTTP Client using the following example:
$symfonyPsr18Client = (new \Symfony\Component\HttpClient\Psr18Client())->withOptions([
'base_uri' => 'https://localhost:9200',
'auth_basic' => ['admin', getenv('OPENSEARCH_PASSWORD')],
'verify_peer' => false,
'max_retries' => 2,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
]);
$serializer = new \OpenSearch\Serializers\SmartSerializer();
$requestFactory = new \OpenSearch\RequestFactory(
$symfonyPsr18Client,
$symfonyPsr18Client,
$symfonyPsr18Client,
$serializer,
);
$transport = (new \OpenSearch\TransportFactory())
->setHttpClient($symfonyPsr18Client)
->setRequestFactory($requestFactory)
->create();
$client = new \OpenSearch\Client($transport, $endpointFactory, []);
// Send a request to the 'info' endpoint.
$info = $client->info();