idoit / zenkit-api-client
Zenkit的PHP API客户端库。
0.3.2
2021-03-14 16:08 UTC
Requires
- php: >=7.1
- ext-json: *
- guzzlehttp/guzzle: ^5.3|^6.2|^7.0
- netresearch/jsonmapper: ^2.0|^3.0|^4.0
- psr/log: ^1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.18
- phpro/grumphp-shim: ^1.3
- psalm/phar: ^4.6
Suggests
- monolog/monolog: For logging API requests and responses and also the mapping process.
README
Zenkit的PHP API客户端库。为了使用API,您需要从Zenkit获取API密钥,您可以在Zenkit API文档中了解相关信息。
请注意,API密钥始终与用户相关联,因此如果您计划开发一个需要处理您可能无权查看的数据的应用程序,您可能需要创建一个具有访问所有工作空间权限的“系统”用户。
测试连接
为了测试您的连接,您可以简单地调用有关您用户的信息
$apiKey = 'your-api-key'; try { // Get the response from Guzzle. $response = (new API($apiKey))->request('auth/currentuser'); // Get the body contents of the response. $rawResponseBody = $response->getBody()->getContents(); // Output data as array. var_dump(json_decode($rawResponseBody, true)); } catch (idoit\zenkit\BadResponseException $e) { echo 'Exception! The status was ' . $e->getCode() . ', response: ' . $e->getResponse()->getBody()->getContents(); } catch (Exception $e) { echo 'Exception! Somewthing else went wrong: ' . $e->getMessage(); }
如果您看到包含有关您用户的信息的数组,则表示成功 - 您应该看到用户ID、短ID、UUID、首字母缩写、全名、最近的项目列表等许多其他信息。
注意事项
命名
在API上下文中,一些组件具有不同的名称,例如“Collections”被称为“Lists”,“Fields”被称为“Elements”。
有关此信息,请参阅API文档。
映射/原始响应
默认情况下,您的响应将被“映射”到类实例。这意味着所有数据(“Elements”、“Entries”、“Lists”...)都将映射到相应的*Item
类实例(ElementItem
、EntryItem
、ListItem
、...)。
检索对象将帮助您在代码自动完成和查看类时处理Zenkit数据。
如果您 -然而- 倾向于处理原始数据而不是映射对象,您有两个选择
- 您可以直接使用
$api->request()
方法并使用Guzzle响应对象进行处理 - 您可以通过
setRawOutput
方法将API切换为在通过服务类使用时提供原始数据
条目/元素定义
为了将“元素”映射到条目并读取其内容,您需要首先加载元素,然后将其传递到条目服务,如下所示
$listShortId = 'your-list-short-id'; $elements = $api->getElementService() ->getElementsInList($listShortId); $entries = $api->getEntryService() ->setElementConfiguration($elements) ->getEntriesForListView($listShortId);
如果没有传递配置,则不会得到映射的条目数据 - 您只能在检索“原始”结果时处理数据。