bdus-db/bdus-api-php-client

Bradypus API的一个简单易用的PHP客户端

1.0.0 2021-11-30 14:04 UTC

This package is auto-updated.

Last update: 2024-09-29 05:49:50 UTC


README

一个开源客户端,用PHP编写,用于与BraDypUS API数据库交互。

这个库只是对BraDypUS API的一个薄包装,该API在docs.bdus.cloud/api/上完全文档化。

如何使用此库的完整示例可以在该存储库的测试文件夹中找到。

目录

用法

安装和设置

BdusApiPhoClient可以通过Composer或手动安装。通过Composer,需要并下载库

composer require bdus-db/bdus-api-php-client

然后通过自动加载器加载它

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

use BdusApiPhpClient\BdusApiPhpClient;

对于手动安装,下载库并要求它

<?php
require_once 'BdusApiPhpClient/src/BdusApiPhpClient.php';
use BdusApiPhpClient\BdusApiPhpClient;

初始化类

$api = new BdusApiPhpClient('https://bdus.cloud/db/api/', 'ghazni');

获取API (BradypUS) 版本

$api->getApiVersion();

返回

[
  'version' => '4.0.0-alpha.220',
]

获取列的唯一值

// Gets unique values for column `provevance` of table `finds`
$api->getUniqueVal('finds', 'provevance');

返回

[
  'Tapa Sardar, Vihara 17',
  'Ghazni, Ghaznavid Palace Area',
  'Ghazni, Ghaznavid Palace',
  'Tapa Sardar, Room 36',
  'Tapa Sardar, Upper Terrace',
  'Tapa Sardar, II terrace',
  'Tapa Sardar, Area 64-100',
  ...
]

获取列的唯一值,带建议

// Gets unique values for column `provevance` of table `finds`, get only values containing `Tapa`
$api->getUniqueVal('finds', 'provevance', 'Tapa');

返回

[
  'Tapa Sardar, Vihara 17',
  'Tapa Sardar, Room 36',
  'Tapa Sardar, Upper Terrace',
  'Tapa Sardar, II terrace',
  'Tapa Sardar, Area 64-100',
  ...,
]

获取列的唯一值,带筛选和建议

// Gets unique values for column `provevance` of table `finds`, get only values containing `Tapa` and limit search only to records having `Late period` in `relative_chronology` column
$api->getUniqueVal('finds', 'provevance', 'Tapa', 'relative_chronology|=|Late period');

返回

[
  'Tapa Sardar, Vihara 17',
  'Tapa Sardar, Room 36',
  'Tapa Sardar, Upper Terrace',
  'Tapa Sardar, Vihara 37',
  'Tapa Sardar, Vihara 23',
  ...,
]

检查数据库配置

$api->inspect();

返回

[
  'finds' =>  [
    'name' => 'ghazni__finds',
    'label' => 'Finds',
    'order' => 'inv_no',
    'id_field' => 'inv_no',
    'preview' =>  [
      'inv_no',
      'archaeological_context',
      ...
    ],
    'plugin' =>  [
      'ghazni__m_biblio',
      'ghazni__m_inscriptions',
      ...
    ],
    'link' =>  [
      [
        'other_tb' => 'ghazni__funcomplex',
        'fld' =>  [
          [
            'my' => 'funcomplex',
            'other' => 'id',
          ]
        ]
      ]
    ],
    'fields' =>  [
      'id' =>  [
        'name' => 'id',
        'label' => 'ID',
        ...
      ],
      'creator' =>  [
        'name' => 'creator',
        'label' => 'Creator',
        'type' => 'text',
        'hide' => '1',
        'fullname' => 'ghazni__finds:ghazni__finds',
      ],
      'inv_no' => [
        'name' => 'inv_no',
        'label' => 'Inventory no.',
        'type' => 'text',
        'check' => [
          'no_dupl',
        ],
        'fullname' => 'ghazni__finds:ghazni__finds',
      ],
      'archaeological_context' =>  [
        'name' => 'archaeological_context',
        'label' => 'Cultural context',
        'type' => 'combo_select',
        'vocabulary_set' => 'archaeo_context',
        'fullname' => 'ghazni__finds:ghazni__finds',
      ],
      'provenance' => [
        'name' => 'provenance',
        'label' => 'Provenance',
        'type' => 'combo_select',
        'get_values_from_tb' => 'ghazni__finds:provenance',
        'fullname' => 'ghazni__finds:ghazni__finds',
      ],
      ...
    ],
    'stripped_name' => 'finds',
  ],
  'excavations' => [
    'name' => 'ghazni__excavations',
    'label' => 'Excavations',
    'order' => 'name',
    'id_field' => 'name',
    'preview' => [
      'name',
      'authors',
      'start_year',
      'end_year',
    ],
    'fields' => [
      'id' => [
        'name' => 'id',
        'type' => 'text',
        'hide' => true,
        'fullname' => 'ghazni__excavations:ghazni__excavations',
      ],
      'name' => [
        'name' => 'name',
        'label' => 'Name',
        'type' => 'text',
        'check' => [
          'no_dupl',
        ],
        'fullname' => 'ghazni__excavations:ghazni__excavations',
      ],
      ...
    ],
    'stripped_name' => 'excavations',
  ],
  'funcomplex' => [
    ...
  ],
  'bibliography' => [
    ...
  ],
  'files' => [
    ...
  ],
  'm_biblio' => [
    ...
  ]
]

检查单个表配置

$api->inspect('finds');

返回

[
  'stripped_name' => 'finds',
  'name' => 'ghazni__finds',
  'label' => 'Finds',
  'order' => 'inv_no',
  'id_field' => 'inv_no',
  'preview' => [
    'inv_no',
    ...
  ]
  ...
]

通过提供数组进行搜索

注意:搜索结果始终分页

$api->searchShortSqlObj([
      'tb' => 'finds',
      'cols' => 'inv_no,archaeological_context,provenance,relative_chronology',
      'where' => [
          [
            'fld' => 'archaeological_context',
            'operator' => '=',
            'value' => 'Buddhist'
          ],
          [
            'connector' => 'and',
            'fld' => 'provenance',
            'operator' => 'like',
            'value' => 'Tapa Sardar'
          ],
        ],
      ]);

返回

[
  'head' => [
    'total_rows' => 357,
    'total_pages' => 12,
    'stripped_table' => 'finds',
    'table_label' => 'Finds',
    'page' => 1,
    'no_records_shown' => 30,
    'fields' => [
      'id' => 'ID',
      'creator' => 'Creator',
      'inv_no' => 'Inventory no.',
      'archaeological_context' => 'Cultural context',
      ...
    ],
  ],
  'debug' => false,
  'records' => [
    [
      'id' => '168',
      'creator' => '11',
      'inv_no' => 'TS00038',
      'archaeological_context' => 'Buddhist',
      ...
    ],
    [
      'id' => '678',
      'creator' => '11',
      ...
    ]
  ]
]

通过提供数组和分页进行搜索

注意:搜索结果始终分页

$api->searchShortSqlObj([
      'tb' => 'finds',
      'cols' => 'inv_no,archaeological_context,provenance,relative_chronology',
      'where' => [
          [
            'fld' => 'archaeological_context',
            'operator' => '=',
            'value' => 'Buddhist'
          ],
          [
            'connector' => 'and',
            'fld' => 'provenance',
            'operator' => 'like',
            'value' => 'Tapa Sardar'
          ],
        ],
      ], [
        'page' => 2
      ]);

返回

[
  'head' => [
    'total_rows' => 357,
    'total_pages' => 12,
    'stripped_table' => 'finds',
    'table_label' => 'Finds',
    'page' => 2,
    ...
  ]
  ...
]

通过提供ShortSQL进行搜索

$api->searchShortSql('@finds~?archaeological_context|=|Buddhist||and|provenance|like|Tapa Sardar');

这实际上与前面的查询相同,作为数组提供

通过提供ShortSQL和分页进行搜索

$api->searchShortSql('@finds~?archaeological_context|=|Buddhist||and|provenance|like|Tapa Sardar', ['page' => 2]);

这实际上与前面的查询相同,作为数组提供

通过ID获取一条记录

// Get record with id = 1 from table finds
$api->getOne('finds', '1');

结果

[
  'metadata' => [
    'tb_id' => 'ghazni__finds',
    'rec_id' => [
      'name' => 'id',
      'label' => 'ID',
      'val' => '1',
    ],
    'tb_stripped' => 'finds',
    'tb_label' => 'Finds',
  ],
  'core' => [
    'id' => [
      'name' => 'id',
      'label' => 'ID',
      'val' => '1',
    ],
    'creator' => [
      'name' => 'creator',
      'label' => 'Creator',
      'val' => NULL,
    ],
    'inv_no' => [
      'name' => 'inv_no',
      'label' => 'Inventory no.',
      'val' => 'TS01092',
    ],
    'archaeological_context' => [
      'name' => 'archaeological_context',
      'label' => 'Cultural context',
      'val' => 'Buddhist',
    ],
    ...
  ],
  'plugins' => [
    'ghazni__m_biblio' => [
      'metadata' => [
        'tb_id' => 'ghazni__m_biblio',
        'tb_stripped' => 'm_biblio',
        'tb_label' => 'Bibliographic data',
        'tot' => 2,
      ],
      'data' => [
        25 => [
          'id' => [
            'name' => 'id',
            'label' => false,
            'val' => '25',
          ],
          'table_link' => 
          ...
        ],
      ],
    ],
  ],
  'links' =>  [ ],
  'backlinks' =>  [ ],
  'manualLinks' => [ ],
  'files' =>  [
    [
      'id' => '3',
      'creator' => NULL,
      'ext' => 'jpg',
      'keywords' => NULL,
      'description' => NULL,
      'printable' => NULL,
      'filename' => 'TS 1092',
    ],
    ...
  ],
  'geodata' =>  [ ],
  'rs' =>  [ ],
]