sitecrafting/gearlab-tools-php

v2.0.0-rc.02 2021-01-04 22:32 UTC

README

Travis CI build status

GearLab Tools REST API 的官方 PHP SDK。

内容

要求

PHP 7.1 或更高版本

安装

Composer

通过 Composer 安装

composer require sitecrafting/gearlab-tools-php

测试

要运行单元测试

composer install
./vendor/bin/phpunit

入门

请遵循安装过程,然后运行以下命令

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// key, baseUri, and collection are all required.
$client = new Client([
  'key'        => $in->getOption('key'),
  'baseUri'    => $in->getOption('base-uri'),
  'collection' => $in->getOption('collection'),
]);

$searchResults = $client->search([
  'query'        => 'trees',   // required
  'resOffset'    => 10,        // defaults to 0
  'resLength'    => 42,        // defaults to 10
  'metaTag'      => 'SO-META', // defaults to ""
  'literalQuery' => true,      // defaults to false
]);

$searchCompletions = $client->completions([
  'prefix'  => 'spongeb',
  'metaTag' => 'SO-META', // defaults to ""
]);

?>

示例结果

搜索

array (
  'results' =>
  array (
    0 =>
    array (
      'url' => 'https://bouldercolorado.gov/civic-area/trees-in-the-civic-area-2',
      'title' => 'Trees in the Civic Area',
      'snippet' => 'Updates on plans for trees in the Boulder Civic Area.',
      'meta' =>
      array (
      ),
    ),
    1 =>
    array (
      'url' => 'https://bouldercolorado.gov/forestry/best-trees',
      'title' => 'Best Trees for Boulder',
      'snippet' => 'Select the right tree for the right place.',
      'meta' =>
      array (
      ),
    ),

    /* ... */

    9 =>
    array (
      'url' => 'https://bouldercolorado.gov/forestry/apps',
      'title' => 'Forestry and Parks Web Apps and Maps!',
      'snippet' => 'Forestry Apps and Maps',
      'meta' =>
      array (
      ),
    ),
  ),
  'recommendations' =>
  array (
    0 =>
    array (
      'url' => 'https://bouldercolorado.gov/open-data/city-of-boulder-public-trees/',
      'title' => 'Public Trees',
      'snippet' => 'Public Trees managed by the Urban Forestry Division of the Department of Parks and Recreation, City of Boulder. The provided tree data does not include trees on Open Space and Mountain Parks (OSMP) land. City ordinances prohibit the picking of fruit on trees located on OSMP property. For a list of u',
      'meta' => NULL,
    ),
  ),
  'resOffset' => 0,
  'resCount' => 10,
  'resStart' => 1,
  'resEnd' => 10,
  'total' => 110,
  'literalQuery' => false,
  'originalQueryPhrase' => 'trees',
  'suggestionSupersededQuery' => false,
  'supersedingSuggestion' => '',
)

补全

array (
  'results' =>
  array (
    0 =>
    array (
      'title' => 'Spongebob Squarepants',
    ),
    1 =>
    array (
      'title' => 'Spongebill Roundhat',
    ),
  ),
)

命令行界面

只需一个 API 密钥和一个集合 ID,您就可以从命令行向 API 发送请求。假设您已克隆了存储库并运行了 composer install,您可以从存储库根目录运行以下命令

bin/gearlab

这将显示 CLI 所有可用子命令的帮助文本。您也可以通过运行 bin/gearlab help <cmd>bin/gearlab <cmd> -h 来获取任何子命令的帮助。

架构

此包包含由Swagger Codegen项目自动生成的 PHP 代码,该代码基于GearLab Tool API 规范。当规范在 Swagger Hub 中保存时,会自动导出生成的代码,该代码包含在此存储库的 swagger/SwaggerClient-php 目录中。Composer 自动加载器配置为自动加载此代码

{
    "autoload": {
        "psr-4": {
          "Swagger\\Client\\": "swagger/Swagger-Client-php/lib/",
          "GearLab\\Api\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
          "Swagger\\Client\\" : "swagger/Swagger-Client-php/test/",
          "GearLab\\Api\\": "test/"
        }
    }
}

GearLab\Api\Client类是所有 API 调用的主要入口点,并标准化了自动生成代码的使用。例如,它将数组查询参数转换为自动生成代码期望的位置参数。

  • API 版本:v2
  • 构建包:io.swagger.codegen.languages.PhpClientCodegen