guym4c / ghost-api-php
PHP Ghost API client
Requires
- ext-json: *
- doctrine/cache: ^1.10
- guzzlehttp/guzzle: ^6.3
- myclabs/php-enum: ^1.7
- psr/http-message: ^1.0
- shrikeh/teapot: ^2.3
Requires (Dev)
README
Easy pulling of content from a Ghost API.
Installation
composer require guym4c/ghost-api-php
Usage
Get an instance of the API client:
$ghost = new Ghost( $yourGhostApiBaseUrl, $yourGhostApiKey, );
You can also pass in a Doctrine Cache provider here (and configure its lifetime), and the client will cache all requests that give a single-resource answer.
Call static methods on the resource classes to get your data.
use Guym4c\GhostApiPhp\Model as Cms; Cms\Post::bySlug($ghost, 'my-post-slug'); Cms\Tag::byId($ghost, 'my-tag-id'); Cms\Page::get()
If you don't know the slug or ID of the resource you're looking for, you can also run queries. (You'll probably want to be reading Ghost's API docs in parallel if you use this method, so that you know what the client is doing.)
Cms\Page::get( /* limit: */ 5, /* simple sorting: */ new Sort('some_value', SortOrder::DESC), );
get()
returns a CollectionRequest
that exposes methods for managing pagination - you can access the returned records using getResources()
.
Filtering
The third parameter to the ::get
method above is a Ghost NQL filter, assembled programmatically using the client. It provides fluent methods that allow you to construct complex filter expressions.
(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()
and else()
are bracket-ised versions of and()
and or()
. You must start with by()
.
We've passed an additional true
at the start here to denote that the value, 'true'
, is a literal and should be interpreted by Ghost as a boolean, not a string.
[]
in-group syntax is not supported by the client.
Refer to the Ghost docs for more information on NQL filtering.