# Strange behavior in PHP Elasticsearch 8.5 API

**URL:** <https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-security, docker\
**Created:** [November 10, 2022, 8:01am UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622 "2022-11-10T08:01:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![abkrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abkrim/32/20217_2.png) [@abkrim](https://discuss.elastic.co/u/abkrim)\
**Post date:** [November 10, 2022, 8:01am UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/1 "2022-11-10T08:01:19Z")

</div>

I feel totally lost.

Having spent some time understanding that to connect to an Elasticsearch single-node, built with docker-compose, without security, I should use the configuration below, now I find that it doesn't work correctly.

I enter perfectly through curl, without CA certificate, without user and without password.

Of course the installation done with docker-compose.yml works.  
That is, I access kibana WITHOUT authorization, user, or enrollment.

## docker-compose.yml

```auto
elasticsearch:
       image: 'docker.elastic.co/elasticsearch/elasticsearch:8.5.0'
       container_name: sitelight-es01
       environment:
           - discovery.type=single-node
# - xpack.license.self_generated.type=basic
           - xpack.security.enabled=false
           - ES_JAVA_OPTS=-Xms1g -Xmx1g
       ulimits:
           memlock:
               soft: -1
               hard: -1
       ports:
           - '9200:9200'
           - '9300:9300'
       volumes:
           - 'sail-elasticsearch:/usr/share/elasticsearch/data'
       networks:
           - sail
   kibana:
       image: 'docker.elastic.co/kibana/kibana:8.5.0'
       container_name: sitelight-kibana
       depends_on:
           - elasticsearch
       environment:
           ELASTICSEARCH_HOSTS: http://sitelight-es01:9200
       ports:
           - '5601:5601'
       networks:
           - sail

```

And I try it in the shell and it works

```auto
curl -XGET "http://localhost:9200/"
{
  "name" : "44edbbb60101",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "6seEH0VRR8mX98dIkzSySg",
  "version" : {
    "number" : "8.5.0",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "c94b4700cda13820dad5aa74fae6db185ca5c304",
    "build_date" : "2022-10-24T16:54:16.433628434Z",
    "build_snapshot" : false,
    "lucene_version" : "9.4.1",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

❯ curl -XPUT "http://localhost:9200/analyzers"
{"acknowledged":true,"shards_acknowledged":true,"index":"analyzers"}%                                                                                                                                                                        
❯ curl -XGET "http://localhost:9200/_cat/indices"
yellow open analyzers zqWTHOgATtO8_1IRr--Ujg 1 1 0 0 225b 225b
❯ curl -XDELETE "http://localhost:9200/analyzers"
{"acknowledged":true}%    

```

## PHP Client

Now I will go to the use of the PHP client

```php
$client = ClientBuilder::create()
   ->setHosts(['localhost'])
   ->build();

$params['index'] = 'analyzers';

$response = $client->info();

```

Throw an error

```auto
08:43:14401 Unauthorized: {"message":"Unauthenticated."}

```

## Change password for user elastic not work.

If I can access via CURL without a username and password, I have no other option than it is a client error, but hey, let's waste a little time trying to turn it around.

Thinking,... well we will have to put the user, its password, etc... we will use what I use in production, to change the password. But not.

I can't understand, because if I try to change the elastic user's password, I can't either.

```auto
sh-5.0$ cd /usr/share/elasticsearch/
sh-5.0$ ls
LICENSE.txt NOTICE.txt README.asciidoc bin config data jdk lib logs modules plugins
sh-5.0$ ./bin/elasticsearch-reset-password -u elastic
WARNING: Owner of file [/usr/share/elasticsearch/config/users] used to be [root], but now is [elasticsearch]
WARNING: Owner of file [/usr/share/elasticsearch/config/users_roles] used to be [root], but now is [elasticsearch]
This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y

ERROR: Failed to reset password for the [elastic] user

```

And now but still. According to the 8.5 documentation [Get users API](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-user.html)

> with `curl -XGET "http://localhost:9200/_security/user"` you should get the list of users and built-in users.

But neither…

```auto
curl -XGET "http://localhost:9200/_security/user"

{"error":"Incorrect HTTP method for uri [/_security/user] and method [GET], allowed: [POST]","status":405}%

```

It is desperation.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 10, 2022, 8:21am UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/2 "2022-11-10T08:21:04Z")

</div>

In the php client, you defined localhost and not localhost:9200. Could it be the issue?

---

<div class="post-metadata">

**Author:** ![abkrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abkrim/32/20217_2.png) [@abkrim](https://discuss.elastic.co/u/abkrim)\
**Post date:** [November 10, 2022, 8:36am UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/3 "2022-11-10T08:36:21Z")

</div>

If use localhost:9220 show my another incredible error:

```auto
09:35:00

No alive nodes. All the 1 nodes seem to be down.

```

## Code

```auto
$client = ClientBuilder::create()
            ->setHosts(['localhost:9200'])
            ->build();

        //$params['index'] = 'analyzers';

        $response = $client->info();

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 10, 2022, 8:57am UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/4 "2022-11-10T08:57:32Z")

</div>

I don't know the PHP client but as you disabled security (which I DO NOT recommend), you might have to switch from https to http?

---

<div class="post-metadata">

**Author:** ![abkrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abkrim/32/20217_2.png) [@abkrim](https://discuss.elastic.co/u/abkrim)\
**Post date:** [November 10, 2022, 9:14am UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/5 "2022-11-10T09:14:37Z")

</div>

> [@dadoonet](#):
>
> I don't know the PHP client but as you disabled security (which I DO NOT recommend), you might have to switch from https to http?

- I use in DOCKER DEVELOP local enviroment.
- I use this for deploy ElasticSeacrh and Kibana with docker-compose.yml

In this environment there is nothing wrong with working like this. Moreover, it is an obligation given that the documentation on how to deploy docker with docker compose is rather confusing and full of problems.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 10, 2022, 9:41am UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/6 "2022-11-10T09:41:24Z")

</div>

> [@abkrim](#):
>
> In this environment there is nothing wrong with working like this.

Well... It won't be the same behavior and code as you will see in production...  
The [documentation](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/connecting.html#auth-http) is pretty clear about security by default.

Anyway. Did you try something like the following?

```auto
$client = ClientBuilder::create()
            ->setHosts(['http://localhost:9200'])
            ->build();
$response = $client->info();

```

---

<div class="post-metadata">

**Author:** ![abkrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abkrim/32/20217_2.png) [@abkrim](https://discuss.elastic.co/u/abkrim)\
**Post date:** [November 10, 2022, 11:32am UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/7 "2022-11-10T11:32:17Z")

</div>

A lot of thanks.

A mistake on trait when I build client for my app, get wrong address for develop environment.

Now is correct and working

---

<div class="post-metadata">

**Author:** ![abkrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abkrim/32/20217_2.png) [@abkrim](https://discuss.elastic.co/u/abkrim)\
**Post date:** [November 10, 2022, 3:57pm UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/8 "2022-11-10T15:57:49Z")

</div>

Well no. It was a fleeting dream.

I don't know how, but I'm still the same, and more desperate, about to do everything without the use of the client, via guzzle that works perfectly for me.

```php
<?php

namespace App\Console\Commands\ELK;

use Elastic\Elasticsearch\ClientBuilder;
use Exception;
use Illuminate\Console\Command;

class TestingElk extends Command
{

    protected $signature = 'elk:test';

    protected $description = 'Dirty code for rapid tests';

    public function handle()
    {
        $client = ClientBuilder::create()
            ->setHosts(['http://localhost:9200'])
            ->build();
        $response = $client->info();
    }
}

```

## Error

> No alive nodes. All the 1 nodes seem to be down.

```auto
$next->markAlive(true);
                return $next;
            }
            $dead++;
        }
 
        throw new NoNodeAvailableException(sprintf(
            'No alive nodes. All the %d nodes seem to be down.',
            $totNodes
        ));
    }
}

```

## Check on shell

```auto
curl -XGET "http://localhost:9200/_cat/indices"
yellow open analyzers 3UFdZFMnRLa6k4AzKrF6_g 1 1 0 0 225b 225b

```

 ![kiban-works](https://us1.discourse-cdn.com/elastic/original/3X/1/b/1b1b10239786b441533ebcb0bde7a9a9cd1d516d.jpeg)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 10, 2022, 4:15pm UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/9 "2022-11-10T16:15:38Z")

</div>

A simple test. Could you try with the default installation of Elasticsearch?  
Without disabling security.

And follow then the PHP client documentation?

So we can at least understand where to look at?

---

<div class="post-metadata">

**Author:** ![abkrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abkrim/32/20217_2.png) [@abkrim](https://discuss.elastic.co/u/abkrim)\
**Post date:** [November 10, 2022, 4:57pm UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/10 "2022-11-10T16:57:47Z")

</div>

After some time get solution.

Well, I use Laravel sail and in doc for use Elasticsearch with docker, I get an idea.

If on command shell of host work fine problem is on side of the container for my app, that not connect with elastic container.

This is the question.

1. I run command from the shell of the host.
2. Code run from conainter larevel app, and in shell of container don't connect to localhost.

## Probe

```auto
# curl -XGET "http://localhost:9200/_cat/indices" 
curl: (7) Failed to connect to localhost port 9200 after 0 ms: Connection refused
# ping sitelight-es01
/bin/sh: 4: ping: not found
# curl -XGET "http://sitelight-es01:9200/
> ^C
# culr -XGET "http://sitelight-es01:9200/"
/bin/sh: 6: culr: not found
# curl -XGET "http://sitelight-es01:9200/"
{
  "name" : "9e89cfc0e2ff",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "6seEH0VRR8mX98dIkzSySg",
  "version" : {
    "number" : "8.5.0",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "c94b4700cda13820dad5aa74fae6db185ca5c304",
    "build_date" : "2022-10-24T16:54:16.433628434Z",
    "build_snapshot" : false,
    "lucene_version" : "9.4.1",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

```

## Solution

The name of the container on elasticsearch on my Laravel Sail project is sitelight-es01, and now it works.

Change my code to

```auto
ClientBuilder::create()
   ->setHosts(['http://sitelight-es01:9200'])
   ->build();

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 8, 2022, 4:58pm UTC](https://discuss.elastic.co/t/strange-behavior-in-php-elasticsearch-8-5-api/318622/11 "2022-12-08T16:58:13Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
