microcmsio/microcms-php-sdk

microCMS PHP SDK

v1.0.1 2021-11-05 05:03 UTC

This package is auto-updated.

Last update: 2022-08-05 07:26:38 UTC


README

microCMS PHP SDK.

Tutorial

See official tutorial.

Installation

$ composer require microcmsio/microcms-php-sdk

Usage

Import

<?php

require_once('vendor/autoload.php');

use \Microcms\Client;

Create client object

$client = new Client(
  "YOUR_DOMAIN",  // YOUR_DOMAIN is the XXXX part of XXXX.microcms.io
  "YOUR_API_KEY"  // API Key
);

Get content list

$list = $client->list("endpoint");
echo $list->contents[0]->title;

Get content list with parameters

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

Get single content

$object = $client->get("endpoint", "my-content-id");
echo $object->title;

Get single content with parameters

$object = $client->get("endpoint", "my-content-id", [
  "draftKey" => "foo",
  "fields" => ["id", "title"],
  "depth" => 1,
]);
echo $object->title;

Get object form content

$object = $client->get("endpoint");
echo $object->title;

Create content

$createResult = $client->create(
  "endpoint",
  [
    "title" => "Hello, microCMS!",
    "contents" => "Awesome contents..."
  ]
);
echo $createResult->id;

Create content with specified ID

$createResult = $client->create(
  "endpoint",
  [
    "id" => "new-my-content-id",
    "title" => "Hello, microCMS!",
    "contents" => "Awesome contents..."
  ]
);
echo $createResult->id;

Create draft content

$createResult = $client->create(
  "endpoint",
  [
    "title" => "Hello, microCMS!",
    "contents" => "Awesome contents..."
  ],
  [ "status" => "draft" ]
);
echo $createResult->id;

Update content

$updateResult = $client->update("endpoint", [
  "id" => "new-my-content-id",
  "title" => "Hello, microCMS PHP SDK!"
]);
echo $updateResult->id;

Update object form content

$updateResult = $client->update("endpoint", [
  "title" => "Hello, microCMS PHP SDK!"
]);
echo $updateResult->id;

Delete content

$client->delete("endpoint", "new-my-content-id");