定制 gathercontent/client 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gathercontent/client

最新稳定版本:1.2.0

Composer 安装命令:

composer require gathercontent/client

包简介

GatherContent client

README 文档

README

Build Status codecov

Supported endpoints

Legacy

Compatible with application/vnd.gathercontent.v0.5+json

These endpoints are essential, so we kept the support for them in this new version. In the future these endpoints will be in the v2 API and we will replace them accordingly.

Current

Compatible with application/vnd.gathercontent.v2+json

Items

Templates

Structures

Folders

Basic usage

To create the GatherContentClient simply pass in a Guzzle client in the constructor.

You will need:

  • your e-mail address to log into GatherContent
  • your API key from GatherContent
<?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;

The listing endpoints are returning pagination data in this new version, you can access it like this:

<?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;

For additional parameters please visit the documentation: /projects/:project_id/items.

The get template endpoint is returning structure object data in this new version, you can access it like this:

<?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;

To create an item with assets, you can do the following:

<?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;

To update an item with assets, you can do the following:

<?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;

Credits

This library has been forked from https://github.com/Cheppers/gathercontent-client and re-released under a new name and ownership of Gathercontent.

统计信息

  • 总下载量: 49.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 6
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2022-10-13