guym4c/ghost-api-php

PHP Ghost API客户端

v2.0.1 2020-06-09 23:10 UTC

This package is auto-updated.

Last update: 2024-09-10 08:37:24 UTC


README

轻松从Ghost API获取内容。

安装

composer require guym4c/ghost-api-php

使用

获取API客户端的实例

$ghost = new Ghost(
    $yourGhostApiBaseUrl,
    $yourGhostApiKey,
);

您还可以在此处传递Doctrine Cache提供者(并配置其生命周期),客户端将缓存所有提供单个资源答案的请求。

调用资源类的静态方法以获取数据。

use Guym4c\GhostApiPhp\Model as Cms;

Cms\Post::bySlug($ghost, 'my-post-slug');
Cms\Tag::byId($ghost, 'my-tag-id');
Cms\Page::get()

如果您不知道要查找的资源slug或ID,您也可以运行查询。(如果您使用此方法,可能需要并行阅读Ghost的API文档,以便了解客户端正在做什么。)

Cms\Page::get(
    /* limit: */ 5,
    /* simple sorting: */ new Sort('some_value', SortOrder::DESC),
);

get()返回一个CollectionRequest,它公开了管理分页的方法 - 您可以使用getResources()访问返回的记录。

过滤

上述::get方法的第三个参数是Ghost NQL过滤器,使用客户端程序化构建。它提供了流畅的方法,允许您构建复杂的过滤表达式。

(new Filter())
    ->by('some-val', '=', 'true', true)
    ->and('another-val', '-', 'not-this')
    ->else((new Filter())
        ->by(
            'this-filter-will-be-given-brackets',
            '=',
            'which-overrides-operator-precedence',
        )
    );

with()else()and()or()的括号化版本。您必须以by()开始。

我们在这里传递了一个额外的true,以表示值'true'是一个字面量,并且应该被Ghost解释为布尔值,而不是字符串。

客户端不支持[]组语法。

有关NQL过滤的更多信息,请参阅Ghost文档。