makina-corpus/nucliadb-php-client

NucliaDB API 的 PHP 客户端

0.2.0 2022-10-12 10:25 UTC

This package is not auto-updated.

Last update: 2024-09-25 19:37:07 UTC


README

免责声明:此库仍在开发中。它被设置用于开发一些 PHP CMS 特定模块,同时保持代码的任何可能部分的通用性。在此状态下,该库仅实现了 Web API 调用的一个子集。请随意提交未实现的 PR 以使此库增长。一些有用的贡献者文档可以在此存储库的 docs/contributors 目录中找到。

使用方法

初始化客户端 API

use Nuclia\ApiClient;

$token = '<your-nucliadb-token>';
$kbid = '<your-nucliadb-knoledgebox-id>';
$zone = '<your-nucliadb-zone>'; //Such as 'europe-1'

$apiClient = new ApiClient($zone, $token, $kbid);

使用搜索 API

搜索 API 旨在反映 NucliaDB 搜索 Web API

创建搜索 API 实例

$searchApi = $apiClient->createSearchApi();

运行简单的全文搜索

use Nuclia\Query\SearchQuery;

$response = $searchApi->search((new SearchQuery())->setQuery('you+shall+not+pass'));

$response 返回一个 Symfony HTTP 客户端响应 实例

使用资源 API

资源 API 旨在反映 NucliaDB 资源 Web API

创建资源 API 实例

$resourcesApi = $apiClient->createResourcesApi();

获取资源。

use Nuclia\Query\GetResourceQuery;

$rid = '<resource-id>'
$response = $resourcesApi->getResource($rid,(new GetResourceQuery()
    ->setShow(EnumArray::show([ShowEnum::VALUES, ShowEnum::BASIC]))
);

创建资源。

$response = $resourcesApi->createResource([
  'title' =>  'The Fellowship of the Ring',
  'links' => [
    'link-1' => [
      'uri' => 'https://en.wikipedia.org/wiki/The_Lord_of_the_Rings:_The_Fellowship_of_the_Ring'
    ]
  ]
]);

更新资源。

$rid = '<resource-id>'
$response = $resourcesApi->modifyResource(
  $rid,
  [
  'title' =>  'The Fellowship of the Ring (Updated)',
  'links' => [
    'link-1' => [
      'uri' => 'https://www.rottentomatoes.com/m/the_lord_of_the_rings_the_fellowship_of_the_ring'
    ]
  ]
]);

删除资源。

$rid = '<resource-id>'
$resourcesApi->deleteResource($rid);

使用资源字段 API

资源字段 API 旨在反映 NucliaDB 资源字段 Web API

创建资源字段 API 实例

$resourceFieldsApi = $apiClient->createResourceFieldsApi();

将二进制文件作为资源字段上传

$body = file_get_contents('The-Fellowship-Of-The-Ring.jpg', 'r');
$md5 = md5($body);
$rid = '<resource-id>'
$fieldId = '<field-id>' // A free string used to identify your file field in resource.

$response = $resourceFieldsApi->uploadBinaryFile($rid, $fieldId, $body, (new UploadBinaryFileHeaders())->setMd5($md5));