0

I am using Elasticsearch 8.1. It is running on port 9200. When I execute from POSTMAN,

https://localhost:9200/learn/_search

with Authorization: Type: Basic Auth and username & password, I can see the result.

But now I want to implement this using PHP. I have installed "elasticsearch/elasticsearch": "^8.0" in my composer. This is the code I have tried.

use Elastic\Elasticsearch\ClientBuilder;

$hosts = [
    'host' => 'localhost',
    'port' => '9200',
    'scheme' => 'https',
    'user' => 'elastic',
    'pass' => 'LisRrIul9oMKh2deJkMv'
];

$client = ClientBuilder::create()
    ->setHosts($hosts)
    ->build();


$params = [
    'index' => 'learn',
];

$results = $client->search($params);

echo "<pre>";
print_r($results);

I am getting

`[Thu Mar 31 22:07:09 2022] 127.0.0.1:44396 [500]: GET / - Uncaught Elastic\Elasticsearch\Exception\ClientResponseException: 404 Not Found: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache/2.4.41 (Ubuntu) Server at localhost Port 80</address>
</body></html>
 in /home/user/Projects/Els/my-apps/esphpsearch/vendor/elasticsearch/elasticsearch/src/Response/Elasticsearch.php:65
Stack trace:
#0 /home/user/Projects/Els/my-apps/esphpsearch/vendor/elasticsearch/elasticsearch/src/Client.php(168): Elastic\Elasticsearch\Response\Elasticsearch->setResponse()
#1 /home/user/Projects/Els/my-apps/esphpsearch/vendor/elasticsearch/elasticsearch/src/Traits/ClientEndpointsTrait.php(1521): Elastic\Elasticsearch\Client->sendRequest()
#2 /home/user/Projects/Els/my-apps/esphpsearch/index.php(24): Elastic\Elasticsearch\Client->search()
#3 {main}
`

2 Answers 2

1

Try this

$client = ClientBuilder::create()
    ->setBasicAuthentication('elastic', 'LisRrIul9oMKh2deJkMv')
    ->setCABundle('/etc/elasticsearch/certs/http_ca.crt')
    ->setHosts(['https://localhost:9200'])
    ->build();

$params = [
    'index' => 'learn',
];

$results = $client->search($params);
Sign up to request clarification or add additional context in comments.

Comments

0

There seems to be something wrong with your ClientBuilder call, since the response indicates that at least the port setting did not work (as it contains localhost Port 80).

Try passing the parameters in one URL string:

$hosts = ['https://elastic:LisRrIul9oMKh2deJkMv@localhost:9200'];

To then get the indices, you can use cat instead of search:

$indices = $client->cat()->indices($params);

3 Comments

I have tried this approach too.
Is the response exactly the same? Is your elasticsearch positively running on the same machine as the webserver?
Yes I am running locally. Elastic search is running in 9200 port. And php in 8000. I am getting result using curl and also in postman

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.