GatherContent 客户端

1.2.0 2023-07-13 13:13 UTC

README

Build Status codecov

支持的端点

旧版

兼容 application/vnd.gathercontent.v0.5+json

这些端点是必需的,因此我们在新版本中保留了对这些端点的支持。将来,这些端点将在 v2 API 中,我们将相应地进行替换。

当前

兼容 application/vnd.gathercontent.v2+json

条目

模板

结构

文件夹

基本用法

要创建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;

版权信息

这个库是从https://github.com/Cheppers/gathercontent-client 分支出来的,并以新的名称和所有权重新发布。