PHPackages                             jsq/amazon-es-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. jsq/amazon-es-php

ActiveLibrary[Search &amp; Filtering](/categories/search)

jsq/amazon-es-php
=================

Support for using IAM authentication with the official Elasticsearch PHP client

0.3.0(6y ago)9310.6M↑29.1%28[1 issues](https://github.com/jeskew/amazon-es-php/issues)[1 PRs](https://github.com/jeskew/amazon-es-php/pulls)13Apache-2.0PHP

Since Mar 20Pushed 3y ago9 watchersCompare

[ Source](https://github.com/jeskew/amazon-es-php)[ Packagist](https://packagist.org/packages/jsq/amazon-es-php)[ RSS](/packages/jsq-amazon-es-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (10)Used By (13)

AWS Auth Elasticsearch-PHP
==========================

[](#aws-auth-elasticsearch-php)

[![Apache 2 License](https://camo.githubusercontent.com/cfbf637c43e838ff0e76ed9fe00ba711f3a40d8e2cedea1d323b2824354d3cd8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a73712f616d617a6f6e2d65732d7068702e7376673f7374796c653d666c6174)](https://www.apache.org/licenses/LICENSE-2.0.html)[![Total Downloads](https://camo.githubusercontent.com/aed624daf893f9bfe2f6160a6bc0a68411c48ea5716c0ae01415410d4fcdbdc3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a73712f616d617a6f6e2d65732d7068702e7376673f7374796c653d666c6174)](https://packagist.org/packages/jsq/amazon-es-php)[![Author](https://camo.githubusercontent.com/297a2017c3c5d5b0f717bf17f2713cd12b5ba24945e861cf9af88e37d6bf50ce/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d406a7265736b65772d626c75652e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/jreskew)[![Build Status](https://camo.githubusercontent.com/71fe448b823c531bdfd393fed3cd683e4e0345b23c86063b494374d8a5e93e07/68747470733a2f2f7472617669732d63692e6f72672f6a65736b65772f616d617a6f6e2d65732d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jeskew/amazon-es-php)

**NB**: SignatureV4 support is built into the Opensearch-PHP client (`opensearch-project/opensearch-php`) as of [version 2.0.1](https://github.com/opensearch-project/opensearch-php/releases/tag/2.0.1). If you are using the Opensearch-PHP client, **you do not need to use this library**.

This package provides a signing handler for use with the official Elasticsearch-PHP client (`elasticsearch/elasticsearch`). By default, the handler will load AWS credentials from the environment and send requestsusing a RingPHP cURL handler.

The search library package must be installed separately. The documentation below will use Elasticsearch-PHP in the examples, but both libraries should be pretty identical.

Basic Usage
-----------

[](#basic-usage)

Instances of `Aws\ElasticsearchService\ElasticsearchPhpHandler` are callables that fulfill Elasticsearch-PHP's handler contract. They can be passed to `Elasticsearch\ClientBuilder`'s `setHandler` method:

```
use Aws\ElasticsearchService\ElasticsearchPhpHandler;
use Elasticsearch\ClientBuilder;

// Create a handler (with the region of your Amazon Elasticsearch Service domain)
$handler = new ElasticsearchPhpHandler('us-west-2');

// Use this handler to create an Elasticsearch-PHP client
$client = ClientBuilder::create()
    ->setHandler($handler)
    ->setHosts(['https://search-foo-3gn4utxfus5cqpn89go4z5lbsm.us-west-2.es.amazonaws.com:443'])
    ->build();

// Use the client as you normally would
$client->index([
    'index' => $index,
    'type' => $type,
    'id' => $id,
    'body' => [$key => $value]
]);
```

Using custom credentials
------------------------

[](#using-custom-credentials)

By default, the handler will attempt to source credentials from the environment as described [in the AWS SDK for PHP documentation](http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/credentials.html). To use custom credentials, pass in a [credential provider](http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/credentials.html#credential-provider):

```
use Aws\Credentials\CredentialProvider;
use Aws\Credentials\Credentials;
use Aws\ElasticsearchService\ElasticsearchPhpHandler;

$provider = CredentialProvider::fromCredentials(
    new Credentials('foo', 'bar', 'baz')
);

$handler = new ElasticsearchPhpHandler('us-west-2', $provider);
```

Using a custom HTTP handler
---------------------------

[](#using-a-custom-http-handler)

By default, the handler will use `Elasticsearch\ClientBuilder::defaultHandler()`to dispatch HTTP requests, but this is customizable via an optional constructor parameter. For example, this repository's tests use a custom handler to mock network traffic:

```
class ElasticsearchPhpHandlerTest extends \PHPUnit_Framework_TestCase
{
    public function testSignsRequestsPassedToHandler()
    {
        $toWrap = function (array $ringRequest) {
            $this->assertArrayHasKey('X-Amz-Date', $ringRequest['headers']);
            $this->assertArrayHasKey('Authorization', $ringRequest['headers']);
            $this->assertStringStartsWith(
                'AWS4-HMAC-SHA256 Credential=',
                $ringRequest['headers']['Authorization'][0]
            );

            return $this->getGenericResponse();
        };
        $handler = new ElasticsearchPhpHandler('us-west-2', null, $toWrap);

        $client = \Elasticsearch\ClientBuilder::create()
            ->setHandler($handler)
            ->build();

        $client->get([
            'index' => 'index',
            'type' => 'type',
            'id' => 'id',
        ]);
    }
    ...
}
```

Installation
------------

[](#installation)

### Composer

[](#composer)

```
composer require jsq/amazon-es-php elasticsearch/elasticsearch:"
