Meilisearch Light PHP Client is a PHP library for using a Meilisearch server in PHP.
PHP 5.6+ compatible unlike official clients (meilisearch-php and meilisearch-symfony) that allow PHP 7.4 or 8.0.
- Meilisearch server URL
- Search API Key
- Admin API Key
With Composer, run this command:
composer require shevabam/meilisearch-light-php-client
First, include the library in your code using the Composer autoloader:
require 'vendor/autoload.php';
Then, create an MeilisearchLightClient object with some paremeters:
$host = 'http://xxx.xxx.xxx.xxx:7700';
$searchKey = 'yyy';
$adminKey = 'zzz';
$Request = new MeilisearchLighClient\Request($host);
Make a call:
$params = ['key' => $adminKey];
$Request->call($params, 'GET', 'indexes');
The call
method takes as a parameter:
- the parameters (see below)
- the HTTP method (GET, POST, PUT, ...)
- the endpoint (corresponds to the Meilisearch query)
- the data to transmit: can be a file (must start with @) or an array
The parameters allow you to specify the API key to use (search or admin) as well as the headers if necessary:
$params = [
'key' => $searchKey,
'headers' => ['Content-type: application/json'],
];
To check that a request is valid, use the isOk()
method:
if ($Request->isOk())
{
$Response = $Request->getResponse();
var_dump($Response->get()); // Response content
}
else
{
echo $Request->getHttpStatus();
}
By default, the return is an object. To get an array:
$Response = $Request->getResponse(true);
<?php
require 'vendor/autoload.php';
$host = 'http://xxx.xxx.xxx.xxx:7700';
$searchKey = 'yyy';
$adminKey = 'zzz';
$Request = new MeilisearchLightClient\Request($host);
$params = ['key' => $adminKey];
$Request->call($params, 'GET', 'indexes');
$response_content = null;
if ($Request->isOk())
{
$Response = $Request->getResponse(true);
$response_content = $Response->get();
}
else
{
echo $Request->getHttpStatus();
}
echo '<pre>'; print_r($response_content);
<?php
require 'vendor/autoload.php';
$host = 'http://xxx.xxx.xxx.xxx:7700';
$searchKey = 'yyy';
$adminKey = 'zzz';
$Request = new MeilisearchLightClient\Request($host);
$params = [
'key' => $adminKey,
'headers' => ['Content-type: application/json'],
];
$Request->call($params, 'POST', 'indexes/movies/documents', '@movies.json');
$response_content = null;
if ($Request->isOk())
{
$Response = $Request->getResponse(true);
$response_content = $Response->get();
}
else
{
echo $Request->getHttpStatus();
}
echo '<pre>'; print_r($response_content);
<?php
require 'vendor/autoload.php';
$host = 'http://xxx.xxx.xxx.xxx:7700';
$searchKey = 'yyy';
$adminKey = 'zzz';
$Request = new MeilisearchLightClient\Request($host);
$params = [
'key' => $adminKey,
'headers' => ['Content-type: application/json'],
];
$datas = [
[
'id' => 1,
'title' => 'Sharknado',
],
[
'id' => 2,
'title' => 'Phone Booth',
],
[
'id' => 3,
'title' => 'Jurassic Park',
],
];
$Request->call($params, 'POST', 'indexes/movies/documents', $datas);
$response_content = null;
if ($Request->isOk())
{
$Response = $Request->getResponse(true);
$response_content = $Response->get();
}
else
{
echo $Request->getHttpStatus();
}
echo '<pre>'; print_r($response_content);
<?php
require 'vendor/autoload.php';
$host = 'http://xxx.xxx.xxx.xxx:7700';
$searchKey = 'yyy';
$adminKey = 'zzz';
$Request = new MeilisearchLightClient\Request($host);
$params = [
'key' => $searchKey,
'headers' => ['Content-type: application/json'],
];
$Request->call($params, 'POST', 'indexes/movies/search', [
'q' => 'jurassic',
'limit' => 50,
'filter' => 'release_date > '.strtotime(date('2018-01-01')),
]);
$response_content = null;
if ($Request->isOk())
{
$Response = $Request->getResponse(true);
$response_content = $Response->get();
}
else
{
echo $Request->getHttpStatus();
}
echo '<pre>'; print_r($response_content);