cheppers/gathercontent-client

GatherContent 客户端

v2.0.7 2021-07-07 10:49 UTC

This package is auto-updated.

Last update: 2024-08-29 04:48:10 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;