microcmsio / microcms-php-sdk
microCMS PHP SDK
v1.0.1
2021-11-05 05:03 UTC
Requires
- ext-json: *
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- phpstan/phpstan: ^0.12.99
- phpunit/phpunit: ^9.5
- psy/psysh: @stable
README
microCMS PHP SDK。
教程
查看 官方教程。
安装
$ composer require microcmsio/microcms-php-sdk
使用
导入
<?php require_once('vendor/autoload.php'); use \Microcms\Client;
创建客户端对象
$client = new Client( "YOUR_DOMAIN", // YOUR_DOMAIN is the XXXX part of XXXX.microcms.io "YOUR_API_KEY" // API Key );
获取内容列表
$list = $client->list("endpoint"); echo $list->contents[0]->title;
带有参数的内容列表
$list = $client->list("endpoint", [ "draftKey" => "foo", "limit" => 10, "offset" => 1, "orders" => ["createdAt", "-updatedAt"], "q" => "PHP", "fields" => ["id", "title"], "filters" => "title[contains]microCMS", "depth" => 1 ]); echo $list->contents[0]->title;
获取单个内容
$object = $client->get("endpoint", "my-content-id"); echo $object->title;
带有参数的单个内容
$object = $client->get("endpoint", "my-content-id", [ "draftKey" => "foo", "fields" => ["id", "title"], "depth" => 1, ]); echo $object->title;
从内容获取对象
$object = $client->get("endpoint"); echo $object->title;
创建内容
$createResult = $client->create( "endpoint", [ "title" => "Hello, microCMS!", "contents" => "Awesome contents..." ] ); echo $createResult->id;
使用指定ID创建内容
$createResult = $client->create( "endpoint", [ "id" => "new-my-content-id", "title" => "Hello, microCMS!", "contents" => "Awesome contents..." ] ); echo $createResult->id;
创建草稿内容
$createResult = $client->create( "endpoint", [ "title" => "Hello, microCMS!", "contents" => "Awesome contents..." ], [ "status" => "draft" ] ); echo $createResult->id;
更新内容
$updateResult = $client->update("endpoint", [ "id" => "new-my-content-id", "title" => "Hello, microCMS PHP SDK!" ]); echo $updateResult->id;
更新内容对象
$updateResult = $client->update("endpoint", [ "title" => "Hello, microCMS PHP SDK!" ]); echo $updateResult->id;
删除内容
$client->delete("endpoint", "new-my-content-id");