Skip to content

How to use search service PHP client

We prepared PHP client for easy integration your project with our search service.

Installation

Source code you can find on github.com/logshub/search-client-php

composer require logshub/search-client-php

Example

<?php
include_once 'vendor/autoload.php';

$location = 'uk01';
$apihash = 'xizinISDPH';
$apiSecret = 'ZJLnvZAujIBOTiJVyfImZYLEKdesJPKUntfFztYLiefQsCMgmd';
$serviceId = '5b5a06d2-23bc-4186-4d1b-3a17bfa7200d';
$client = \Logshub\SearchClient\Client::fromLocation(
   $location, $apihash, $apiSecret);

$product = new \Logshub\SearchClient\Model\Product('1', [
   'name' => 'Laptop Full HD 8GB RAM',
   'price' => 899
]);
$request = new \Logshub\SearchClient\Request\IndexProducts($serviceId, [
   $product
]);
$response = $client->indexProducts($request);

echo $response->getBody() . PHP_EOL;

Quick Start

We prepared terminal's commands for you to quickly test your service. This is only for testing purpose, you do not need this commands for integration. After installation with composer, you can find it in vendor/bin/logshub-search.

Execute php vendor/bin/logshub-search list for full list of available commands. All the commands require configuration file with your service's details. The following examples assume that it is placed in /etc/logshub-search.ini. Paste your service's deatils in INI format, eg.:

serviceid = "5b5a06d2-23bc-4186-4d1b-3a17bfa7200d"
location = "uk01"
apihash = "xizinISDPH"
apisecret = "ZJLnvZAujIBOTiJVyfImZYLEKdesJPKUntfFztYLiefQsCMgmd"
pub_key = "HokESHG"

Now, we can execute commands, by referencing to this configuration file.

php vendor/bin/logshub-search index \
--config "/etc/logshub-search.ini" \
--id 1 \
--name "Laptop Full HD 8GB RAM" \
--price 899

php vendor/bin/logshub-search search \
--config "/etc/logshub-search.ini" \
--query "lap"

Usage

To create a client you need the following details from your logshub.com dashboard:

  • API Hash
  • API Secret
  • Location of your service eg. uk01
<?php
include_once 'vendor/autoload.php';

$location = 'uk01';
$apihash = 'xizinISDPH';
$apiSecret = 'ZJLnvZAujIBOTiJVyfImZYLEKdesJPKUntfFztYLiefQsCMgmd';

$client = \Logshub\SearchClient\Client::fromLocation(
    $location, $apihash, $apiSecret);

Indexing

Indexing is the main operation of this client. The most common flow is to index your documents by PHP, and search by JavaScript on frontend of your website.

<?php

try {
    $product1 = new \Logshub\SearchClient\Model\Product('1', [
        'name' => 'Laptop Full HD 8GB RAM',
        'price' => 899
    ]);
    $product2 = new \Logshub\SearchClient\Model\Product('2', [
        'name' => 'Laptop 4GB RAM',
        'price' => 499
    ]);
    $serviceId = '5b5a06d2-23bc-4186-4d1b-3a17bfa7200d';
    $request = new \Logshub\SearchClient\Request\IndexProducts($serviceId, [
        $product1,
        $product2,
    ]);
    $response = $client->indexProducts($request);

    echo $response->getStatusCode() . PHP_EOL;
    echo $response->getBody() . PHP_EOL;

} catch (\Logshub\SearchClient\Exception $e){
    // ...
}

Searching

Searching by this client is not the main flow. Recommended way is to use JavaScript integration for this. However, this client implements searching in case if you would like to search by your backend.

<?php

$pubKey = 'sDZTiXX';
$query = 'laptop';
$features = 'products,aggcategories';

try {
    $request = new \Logshub\SearchClient\Request\SearchProducts(
        $pubKey,
        $query,
        $features
    );
    $response = $client->searchProducts($request);
    /** @var $product \Logshub\SearchClient\Model\Product */
    foreach ($response->getProducts() as $product){
        echo $product->getName() . ' ' . PHP_EOL;
    }

} catch (\Logshub\SearchClient\Exception $e){
    // ...
}

Deleting

You can delete any document, that was indexed previously.

<?php

$serviceId = '5b5a06d2-23bc-4186-4d1b-3a17bfa7200d';
$documentId = '123';

try {
    $request = new \Logshub\SearchClient\Request\Delete(
        $serviceId,
        $documentId
    );
    $response = $client->deleteDocument($request);

} catch (\Logshub\SearchClient\Exception $e){
    // ...
}

Clearing

You can clear your index from all the document, but be careful with this method :)

<?php

$serviceId = '5b5a06d2-23bc-4186-4d1b-3a17bfa7200d';

try {
    $request = new \Logshub\SearchClient\Request\Clear($serviceId);
    $response = $client->clearIndex($request);

} catch (\Logshub\SearchClient\Exception $e){
    // ...
}

Indexing CSV file

Prepare CSV file with the following structure:

csv example

Execute the command, using our integration PHP client:

php vendor/bin/logshub-search index:csv \
--config /etc/logshub-search.ini \
--csv /home/greg/products.csv