简体   繁体   中英

PHP script to get data from Elasticsearch on another server

I have a PHP script installed on a web host server. It features a search bar and a button. After inputting information into the search bar and clicking the button a query will be sent to Elasticsearch which should return a result.

This worked fine on my local server setup with WAMP. One of the differences here though is that Elasticsearch itself is running on another server (Debian 9).

The Script (with the external IP redacted):

<?php
require_once 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->setHosts(['externalIP:9200'])->build();
$es = $client;

echo "<div class='search'>
        <form action='index.php' method='get' autocomplete='off' class='search_form'> 
            <label>         
                <input type='text' name='q' placeholder='Søk her'>
            </label>
                <label><input type='submit' value='Søk' name='s'></label>
            </form>
        </div>";

if (isset($_GET['q'])) {
    $q = $_GET['q'];
    $query = $es->search([
        'index' => 'aksjeregisteret2017',
        'size' => 20,
        'body' => [
            'query' => [
                'bool' => [
                    'must' => [
                        'multi_match' => [
                            'query' => $q,
                            'fields' => ['message', 'Navn Aksjoner', 'Orgnr', 'Selskap',
                                'Aksjeklasse', 'Navn Aksjoner', 'Fodselar/Orgnr', 'Postnr',
                                'Antall Aksjer', 'Antall Aksjer Selskap'],
                            "minimum_should_match" => "50%"
                        ]
                    ]
                ]
            ]
        ]
    ]);
}

if($query['hits']['total'] >=1 ) {
    $results = $query['hits']['hits'];
}

//var_dump($results);


?>
<!doctype html>
<html>
<head>
    <title>Søk i Aksjoneregisteret</title>
    <link rel="stylesheet" href="css/main.css">
</head>
<body>
    <?php if (isset($_GET['q'])) { ?>
        <table>
            <tr>
                <th>Orgnr</th>
                <th>Selskap</th>
                <th>Aksjeklasse</th>
                <th>Navn Aksjoner</th>
                <th>Fodselar/Orgnr</th>
                <th>Postnr</th>
                <th>Poststed</th>
                <th>Antall Aksjer</th>
                <th>Antall Aksjer Selskap</th>
            </tr>

            <?php foreach ($results as $r) { ?>
                <tr>
                    <td><?php echo $r['_source']['Orgnr'] ?></td>
                    <td><?php echo $r['_source']['Selskap'] ?></td>
                    <td><?php echo $r['_source']['Aksjeklasse'] ?></td>
                    <td><?php echo $r['_source']['Navn Aksjoner'] ?></td>
                    <td><?php echo $r['_source']['Fodselar/Orgnr'] ?></td>
                    <td><?php echo $r['_source']['Postnr'] ?></td>
                    <td><?php echo $r['_source']['Poststed'] ?></td>
                    <td><?php echo $r['_source']['Antall Aksjer'] ?></td>
                    <td><?php echo $r['_source']['Antall Aksjer Selskap'] ?></td>
                </tr>
            <?php } ?>
        </table>
    <?php } else { echo "no result"; } ?>
</body>
</html>

The problem is that instead of returning the table with data it returns nothing. Just a blank page after loading for a while. No errors.

One thing that could be wrong is how we have installed the PHP script. We cannot use a terminal on our web host so we used composer to create the elasticsearch PHP vendor folder on the linux server (the one with Elasticsearch installed) and then moved it directly into a folder on our web host together with the script. Is this supposed to work or are there any problems by doing it this way? Is there another way to do it?

By using the external IP + port (9200) in the browser we get to see this page:

在此处输入图片说明

If I'm not wrong that should mean the connection to Elasticsearch is open? So using the same externalIP + Port in the script should make us able to get a result?

Any ideas about what I've done wrong?

We figured out that the above does not work on the web host server because that server lacks certain features that are required for Elasticsearch PHP.

We moved the script to a VPS and set it up there with little to no modifications and it worked.

If you're going to use Elasticsearch scripts on a web host server where they can't let you install new libraries then make sure they have the requirements to run it first. The requirements for Elasticsearch PHP can be found on this page: https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_installation_2.html

They are:

PHP 7.0.0 or higher
Composer
ext-curl: the Libcurl extension for PHP (see note below)
Native JSON Extensions (ext-json) 1.3.7 or higher

The web host server did not have the Libcurl extension and so the script would not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM