cheppers / gathercontent-client
GatherContent 客户端
v2.0.7
2021-07-07 10:49 UTC
Requires
- guzzlehttp/guzzle: ^6.3
Requires (Dev)
- cheppers/git-hooks: ^0.0.9
- cheppers/lint-report: ^0.0.5
- cheppers/robo-git: ^0.0.5
- cheppers/robo-phpcs: ^0.0.13
- consolidation/robo: ^1.1
- phpunit/phpunit: ^6.2
- webmozart/path-util: ^2.3
README
支持的端点
旧版本
兼容 application/vnd.gathercontent.v0.5+json
这些端点是基本的,所以我们保留了在新版本中对它们的支持。将来这些端点将在 v2 API 中,我们将相应地替换它们。
- GET: /me
$gc->meGet()
- GET: /accounts
$gc->accountsGet()
- GET: /accounts/:account_id
$gc->accountGet()
- GET: /projects
$gc->projectsGet()
- GET: /projects/:project_id
$gc->projectGet()
- POST: /projects
$gc->projectsPost()
- GET: /projects/:project_id/statuses
$gc->projectStatusesGet()
- GET: /projects/:project_id/statuses/:status_id
$gc->projectStatusGet()
- POST: /items/:item_id/choose_status
$gc->itemChooseStatusPost()
当前
兼容 application/vnd.gathercontent.v2+json
项目
- GET: /projects/:project_id/items
$gc->itemsGet()
- GET: /items/:item_id
$gc->itemGet()
- POST: /projects/:project_id/items
$gc->itemPost()
- POST: /items/:item_id/content
$gc->itemUpdatePost()
- POST: /items/:item_id/rename
$gc->itemRenamePost()
- POST: /items/:item_id/move
$gc->itemMovePost()
- POST: /items/:item_id/apply_template
$gc->itemApplyTemplatePost()
- POST: /items/:item_id/disconnect_template
$gc->itemDisconnectTemplatePost()
- POST: /items/:item_id/duplicate
$gc->itemDuplicatePost()
模板
- GET: /projects/:project_id/templates
$gc->templatesGet()
- GET: /templates/:template_id
$gc->templateGet()
- POST: /projects/:project_id/templates
$gc->templatePost()
- DELETE: /templates/:template_id/delete
$gc->templateDelete()
- POST: /templates/:template_id/rename
$gc->templateRenamePost()
- POST: /templates/:template_id/duplicate
$gc->templateDuplicatePost()
结构
- GET: /structures/:structure_uuid
$gc->structureGet()
- PUT: /structures/:structure_uuid
$gc->structureAlterPut()
- POST: /structures/:structure_uuid/save_as_template
$gc->structureSaveAsTemplatePost()
文件夹
- GET: /folders
$gc->foldersGet()
- POST: /folders
$gc->folderPost()
- POST: /folders
$gc->folderRenamePost()
- POST: /folders
$gc->folderMovePost()
- DELETE: /folders
$gc->folderDelete()
- POST: /folders
$gc->folderRestorePost()
基本用法
创建GatherContentClient只需在构造函数中传递一个Guzzle客户端。
您需要
- 登录到GatherContent的电子邮件地址
- 来自GatherContent的API密钥
<?php $email = 'YOUR_GATHERCONTENT_EMAIL'; $apiKey = 'YOUR_GATHERCONTENT_API_KEY'; $client = new \GuzzleHttp\Client(); $gc = new \Cheppers\GatherContent\GatherContentClient($client); $gc ->setEmail($email) ->setApiKey($apiKey); try { $me = $gc->meGet(); } catch (\Exception $e) { echo 'ERROR: ' . $e->getMessage() . PHP_EOL; exit(1); } echo "Email = {$me->email}" . PHP_EOL; echo "First name = {$me->firstName}" . PHP_EOL; echo "Last name = {$me->lastName}" . PHP_EOL;
在此新版本中,列表端点正在返回分页数据,您可以通过以下方式访问它
<?php $email = 'YOUR_GATHERCONTENT_EMAIL'; $apiKey = 'YOUR_GATHERCONTENT_API_KEY'; $client = new \GuzzleHttp\Client(); $gc = new \Cheppers\GatherContent\GatherContentClient($client); $gc ->setEmail($email) ->setApiKey($apiKey); try { $projectId = 12345; $items = $gc->itemsGet($projectId); } catch (\Exception $e) { echo 'ERROR: ' . $e->getMessage() . PHP_EOL; exit(1); } $firstItem = reset($items['data']); echo "First content's name = {$firstItem->name}" . PHP_EOL; echo "Pagination total = {$items['pagination']->total}" . PHP_EOL; echo "Pagination current page = {$items['pagination']->currentPage}" . PHP_EOL;
有关其他参数的详细信息,请参阅文档:/projects/:project_id/items。
在此新版本中,获取模板端点正在返回结构对象数据,您可以通过以下方式访问它
<?php $email = 'YOUR_GATHERCONTENT_EMAIL'; $apiKey = 'YOUR_GATHERCONTENT_API_KEY'; $client = new \GuzzleHttp\Client(); $gc = new \Cheppers\GatherContent\GatherContentClient($client); $gc ->setEmail($email) ->setApiKey($apiKey); try { $templateId = 12345; $template = $gc->templateGet($templateId); } catch (\Exception $e) { echo 'ERROR: ' . $e->getMessage() . PHP_EOL; exit(1); } echo "Template's name = {$template['data']->name}".PHP_EOL; echo "Structure UUID = {$template['related']->structure->id}".PHP_EOL; $group = reset($template['related']->structure->groups); echo "Structure's first Group's name = {$group->name}".PHP_EOL;
要创建带有资产的项,可以执行以下操作
<?php $email = 'YOUR_GATHERCONTENT_EMAIL'; $apiKey = 'YOUR_GATHERCONTENT_API_KEY'; $client = new \GuzzleHttp\Client(); $gc = new \Cheppers\GatherContent\GatherContentClient($client); $gc ->setEmail($email) ->setApiKey($apiKey); try { $projectId = 12345; $templateId = 12345; $item = $gc->itemPost($projectId, new Item([ 'name' => 'Item name', 'template_id' => $templateId, 'content' => [ 'field-uuid' => 'Body content', ], 'assets' => [ 'file-field-uuid' => [ '/path-to-your-file/test.jpg', '/path-to-your-file/test.txt', ], ], ])); } catch (\Exception $e) { echo 'ERROR: ' . $e->getMessage() . PHP_EOL; exit(1); } echo "Content's name = {$item['data']->name}".PHP_EOL; echo "Item ID = {$item['data']->id}".PHP_EOL; echo "Created assets array = {$item['meta']->assets}".PHP_EOL;
要更新带有资产的项,可以执行以下操作
<?php $email = 'YOUR_GATHERCONTENT_EMAIL'; $apiKey = 'YOUR_GATHERCONTENT_API_KEY'; $client = new \GuzzleHttp\Client(); $gc = new \Cheppers\GatherContent\GatherContentClient($client); $gc ->setEmail($email) ->setApiKey($apiKey); try { $itemId = 12345; $item = $gc->itemUpdatePost($itemId, [ 'field-uuid' => 'Body change', ], [ 'file-field-uuid' => [ '/path-to-your-file/test.jpg', '/path-to-your-file/test.txt', ], ]); } catch (\Exception $e) { echo 'ERROR: ' . $e->getMessage() . PHP_EOL; exit(1); } echo "Created assets array = {$item->assets}".PHP_EOL;