srg/ghost-api

Ghost CMS/发布平台API的PHP封装

v1.0.1 2021-10-07 22:37 UTC

This package is auto-updated.

Last update: 2024-09-08 05:24:48 UTC


README

Ghost CMS/发布平台API的PHP封装

Ghost内容API & Ghost管理员API

PHP封装与GhostContentAPI和GhostAdminAPI的JavaScript SDK具有相同的签名

配置

遵循Ghost的说明进行自定义集成。https://ghost.org/integrations/custom-integrations/

将生成的密钥添加到.env文件中

GHOST_CONTENT_KEY=
GHOST_ADMIN_KEY=
GHOST_URL=
GHOST_VERSION=

添加到Laravel的config/services.php中

'ghost' => [
   'content_key' => env('GHOST_CONTENT_KEY'),
   'admin_key'   => env('GHOST_ADMIN_KEY'),
   'url'         => env('GHOST_URL'),
   'version'     => env('GHOST_VERSION'),
],

使用方法

GhostContentAPI

    $config = [
    'url'    => config('services.ghost.url'),
    'key'    => config('services.ghost.content_key'),
    'version'=> config('services.ghost.version'),
    ];

    $api = new GhostContentAPI($config);
    
    $rs = [];  //hold results

    // Browsing posts returns Promise([Post...]);
    // The resolved array will have a meta property
    $rs[] = $api->posts->browse(['limit' => 2, 'include' => 'tags,authors']);
    $rs[] = $api->posts->browse();

    // Reading posts returns Promise(Post);
    $rs[] = $api->posts->read(['id' => 'abcd1234']);
    $rs[] = $api->posts->read(['slug' => 'something'], ['formats' => ['html', 'plaintext']]);

    // Browsing authors returns Promise([Author...])
    // The resolved array will have a meta property
    $rs[] = $api->authors->browse(['page' => 2]);
    $rs[] = $api->authors->browse();

    // Reading authors returns Promise(Author);
    $rs[] = $api->authors->read(['id' => 'abcd1234']);
    // include can be array for any of these
    $rs[] = $api->authors->read(['slug' => 'something'], ['include' => 'count.posts']);

    // Browsing tags returns Promise([Tag...])
    // The resolved array will have a meta property
    $rs[] = $api->tags->browse(['order' => 'slug ASC']);
    $rs[] = $api->tags->browse();

    // Reading tags returns Promise(Tag);
    $rs[] = $api->tags->read(['id' => 'abcd1234']);
    $rs[] = $api->tags->read(['slug' => 'something'], ['include' => 'count.posts']);

    // Browsing pages returns Promise([Page...])
    // The resolved array will have a meta property
    $rs[] = $api->pages->browse(['limit' => 2]);
    $rs[] = $api->pages->browse();

    // Reading pages returns Promise(Page);
    $rs[] = $api->pages->read(['id' => 'abcd1234']);
    $rs[] = $api->pages->read(['slug' => 'something', 'fields' => ['title']]);

    // Browsing settings returns Promise(Settings...)
    // The resolved object has each setting as a key value pair
    $rs[] = $api->settings->browse();

    //output
    dump($rs);

GhostAdminAPI

    $config = [
    'url'    => config('services.ghost.url'),
    'key'    => config('services.ghost.admin_key'),
    'version'=> config('services.ghost.version'),
    ];

    $api = new GhostAdminAPI($config);
    
    $rs = []; //hold results
    $rs[] = $api->posts->browse();
    $rs[] = $api->posts->read(['id' => 'abcd1234']);
    $rs[] = $api->posts->delete(['id' => 'abcd1234']);
    
    //todo:
    //$rs[] = $api->posts->add(['title' => 'My first API post']);
    //todo:
    //$rs[] = $api->pages->edit(['id'   => 'abcd1234', 'title' => 'Renamed  my post']);
    
    //output
    dump($rs);

所有函数都与TryGhost JavaScript SDK具有相同的签名,参数与Ghost文档中的参数相同。

Ghost API