foobarfighters / zend-server-web-api
PHP 对 Zend Server Web API 的封装
v0.0.5
2021-07-05 15:47 UTC
Requires
- ext-dom: *
- ext-json: *
- ext-libxml: *
- ext-simplexml: *
- ext-zip: *
- guzzlehttp/guzzle: ^7.3
Requires (Dev)
- kint-php/kint: 3.2.2
- monolog/monolog: ^2.2
- phpstan/phpstan: ^0.12.88
- phpunit/phpunit: ^8
README
一个基于 Guzzle 的包装器,用于(部分)Zend Server Web API。不依赖于任何框架。检查 ZendServer/WebApi/Client/Core/Method 中的 traits 以查看当前支持的端点。
安装
可以使用 composer 安装此包
composer require foobarfighters/zend-server-web-api
用法
当使用 ClientFactory 时,可以将 Zend Server 凭据作为数组传递。或者,在手动实例化客户端时,可以直接将这些值传递给客户端构造函数。
<?php $config = [ 'baseUrl' => 'https://your.zend.server.url', //== ZendServer admin > Administration > Web API Keys > UserName 'username' => 'foobar', //== ZendServer admin > Administration > Web API Keys > Hash 'hash' => 'f1ghtErs6fddd00ccec4eb1b261a8f6cc2dad94c1eb100eab', //== see https://help.zend.com/zend/current/content/web_api_reference_guide.htm 'version' => 1.23, ];
核心客户端
核心客户端负责执行 API 请求,将响应作为关联数组返回或抛出异常。
use FooBarFighters\ZendServer\WebApi\Client\ClientFactory; try{ //== instantiate a core client $client = ClientFactory::createClient($config); //== get the raw API data as an associative array $res = $client->applicationGetStatus(); //== do something useful with it print_r($res['responseData']); } //== one size fits all catch(Exception $e){ error_log($e); echo 'stay calm, the internet police has been notified!'; }
扩展客户端
扩展客户端装饰核心客户端,将原始 API 输出映射到数据模型。这些模型提供了额外的功能,如过滤、日期解析和 IDE 中的自动完成。
use FooBarFighters\ZendServer\WebApi\Client\ClientFactory; try{ //== instantiate an extended client $client = ClientFactory::createExtendedClient($config); //== reduce the list of applications to a single app model $myApp = $client->getAppList()->filterByName('myAppName'); //== do something very exciting with it echo $myApp->getId(); } //== several types of specific exceptions can be thrown, like Guzzle or Api related catch(\FooBarFighters\ZendServer\WebApi\Exception\ApiException $e){ echo 'You broke the API!'; }
自定义 Guzzle
可以将自定义 Guzzle 客户端传递给 ClientFactory 或 Client 类。这可以用于添加头部、中间件或自定义处理器。请参阅示例启动文件了解如何添加 Monolog 日志中间件。
$handlerStack = HandlerStack::create(); //== add middleware or custom handlers ... $client = ClientFactory::createExtendedClient($config, new Guzzle(['handler' => $handlerStack]));
示例
有关更多示例,请参阅示例文件夹。
cd vendor/foobarfighters/ZendServer
php -S localhost:8080 -t examples
示例可以调用实际的 Zend Server 实例或以模拟模式运行。
模拟模式
在模拟模式下运行示例时,所有 API 响应都从一组 JSON 文件中注入,而不是连接到 Zend Server 实例。
实时模式
请注意,在实时模式下
- ZS 凭据需要存储在 vhost 环境变量中或更改启动文件中的配置数组。
- 请求/响应记录在日志文件夹中。这需要写入权限。