PHP code example of storyblok / php-management-api-client
1. Go to this page and download the library: Download storyblok/php-management-api-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
storyblok / php-management-api-client example snippets
Storyblok\ManagementApi\ManagementApiClient;
/** @var ManagementApiClient $client */
$client = new ManagementApiClient($storyblokPersonalAccessToken);
use Storyblok\ManagementApi\ManagementApiClient;
use \Storyblok\ManagementApi\Data\Enum\Region;
$client = new ManagementApiClient($storyblokPersonalAccessToken, Region::US);
$clientEU = new ManagementApiClient($accessToken);
$spaceApi = new SpaceApi($clientEU);
// Retrieve all spaces
$response = $spaceApi->all();
// here you can access to `$response` method if you need
$spaces = $response->data();
// Here you can access to the list of spaces via `$spaces`
$clientEU = new ManagementApiClient($accessToken);
$spaceApi = new SpaceApi($clientEU);
$spaces = $spaceApi->all()->data();
$spaces->forEach( function (Space $space) {
printf("SPACE : %s (%s) - %s - created at: %s as %s" ,
$space->id(),
$space->region(),
$space->name(),
$space->createdAt(),
$space->planDescription()
);
echo PHP_EOL;
});
$spaceId = "12345";
$spaceApi = new SpaceApi($clientEU);
$space = $spaceApi->get($spaceId)->data();
printf(" The name for the Space id : %s is : %s . Plan: %s - %s" ,
$spaceId,
$space->name(),
$space->planLevel(),
$space->planDescription()
);
// Create a backup for a space
try {
$response = $spaceApi->backup($spaceID);
if ($response->isOk()) {
echo "BACKUP DONE!";
} else {
echo $response->getErrorMessage() . PHP_EOL;
}
} catch (Exception $e) {
echo "Error, " . $e;
}
use Storyblok\ManagementApi\Endpoints\StoryApi;
use Storyblok\ManagementApi\Data\Story;
use Storyblok\ManagementApi\ManagementApiClient;
use Storyblok\ManagementApi\Endpoints\StoryApi;
$spaceId= "1234";
$client = new ManagementApiClient($storyblokPersonalAccessToken);
$storyApi = new StoryApi($client, $spaceId);
use Storyblok\ManagementApi\Endpoints\StoryApi;
$storyApi = new StoryApi($client, $spaceId);
$stories = $storyApi->page()->data();
foreach ($stories as $story) {
echo $story->id() . " - " . $story->name() . PHP_EOL;
}
use Storyblok\ManagementApi\Endpoints\StoryApi;
$storyApi = new StoryApi($client, $spaceId);
$stories = $storyApi->page(
new StoriesParams(containComponent: "hero-section"),
page: new PaginationParams(2, 10)
)->data();
echo "Stories found: " . $stories->count();
echo " STORY : " . $stories->get("0.name") . PHP_EOL;
use Storyblok\ManagementApi\Endpoints\StoryBulkApi;
use Storyblok\ManagementApi\QueryParameters\Filters\Filter;
use Storyblok\ManagementApi\QueryParameters\Filters\QueryFilters;
$storyBulkApi = new StoryBulkApi($client, $spaceId);
$stories = $storyBulkApi->all(
filters: (new QueryFilters())->add(
new Filter(
"headline",
"like",
"%mars%"
)
)
);
foreach ($stories as $story) {
echo $story->name() . PHP_EOL;
}
$stories = $storyApi->all(
filters: (new QueryFilters())->add(
new Filter(
"component",
"in",
"article-page"
)
)
);
foreach ($stories as $story) {
echo $story->name() . PHP_EOL;
}
$content = new StoryComponent("article-page");
$content->set("title", "My New Article");
$content->set("body", "This is the content");
// $content->setAsset("image", $assetCreated);
$story = new Story(
name: "A Story",
slug: "a-story",
content: $content
);
try{
$storyCreated = $storyApi->create($story)->data();
echo "Created Story: " . $storyCreated->id() . " - " . $storyCreated->name() . PHP_EOL;
} catch (ClientException $e) {
echo "Error while creating story: " . PHP_EOL;
echo $e->getResponse()->getContent(false);
echo PHP_EOL;
echo $e->getMessage();
}
echo PHP_EOL;
$storyApi->publish($storyId);
use Storyblok\ManagementApi\Data\Story;
use Storyblok\ManagementApi\Data\StoryComponent;
use Storyblok\ManagementApi\Endpoints\StoryBulkApi;
use Storyblok\ManagementApi\ManagementApiClient;
$client = new ManagementApiClient($storyblokPersonalAccessToken);
$storyBulkApi = new StoryBulkApi($client, $spaceId);
$file = new SplFileObject("stories.csv");
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl(separator: ";");
$stories = [];
foreach ($file as $row) {
list($slug, $name, $contentType) = $row;
$story = new Story(
$name,
$slug,
new StoryComponent($contentType)
);
$stories[] = $story;
}
$createdStories = iterator_to_array($storyBulkApi->createStories($stories));
use Storyblok\ManagementApi\Endpoints\UserApi;
use Storyblok\ManagementApi\Data\User;
use Storyblok\ManagementApi\Endpoints\UserApi;
use Storyblok\ManagementApi\ManagementApiClient;
$client = new ManagementApiClient(getAccessToken());
$currentUser = (new UserApi($client))->me()->data();
// "User ID"
echo $currentUser->id() . PHP_EOL;
// "User identifier"
echo $currentUser->userid() . PHP_EOL;
// "User email"
echo $currentUser->email() . PHP_EOL;
// "User has Organization"
echo ($currentUser->hasOrganization() ? " HAS ORG:" . $currentUser->orgName() : "NO ORG") . PHP_EOL;
// "User has Partner"
echo ($currentUser->hasPartner() ? " HAS PARTNER" : "NO PARTNER") . PHP_EOL;;
use Storyblok\ManagementApi\Endpoints\AssetApi;
use Storyblok\ManagementApi\Data\Asset;
use Storyblok\ManagementApi\ManagementApiClient;
$client = new ManagementApiClient($storyblokPersonalAccessToken);
$spaceId = "spaceid";
$assetApi = new AssetApi($client, $spaceId);
use Storyblok\ManagementApi\QueryParameters\{AssetsParams,PaginationParams};
$assetApi = new AssetApi($client, $spaceId);
$assets = $assetApi->page(
new AssetsParams(
inFolder: -1,
search: "something"
),
new PaginationParams(1,1000)
)->data();
use Storyblok\ManagementApi\ManagementApiClient;
$client = new ManagementApiClient($storyblokPersonalAccessToken);
$spaceId = "spaceid";
$tagApi = new TagApi($client, $spaceId);
use Storyblok\ManagementApi\Data\Story;
use Storyblok\ManagementApi\ManagementApiClient;
use Storyblok\ManagementApi\Data\StoryComponent;
use Storyblok\ManagementApi\Endpoints\StoryApi;
use Storyblok\ManagementApi\Endpoints\AssetApi;
$client = new ManagementApiClient($storyblokPersonalAccessToken);
$spaceId = "your-space-id";
$storyApi = new StoryApi($client, $spaceId);
$assetApi = new AssetApi($client, $spaceId);
echo "UPLOADING ASSET..." . PHP_EOL;
$assetCreated = $assetApi->upload("image.png")->data();
echo "Asset created, ID: " . $assetCreated->id() . PHP_EOL;
echo "PREPARING STORY CONTENT DATA..." . PHP_EOL;
$content = new StoryComponent("article-page");
$content->set("title", "New Article");
$content->set("body", "This is the content");
$content->setAsset("image", $assetCreated);
echo "INITIALIZING STORY OBJECT..." . PHP_EOL;
$story = new Story(
"An Article",
"an-article-" . random_int(10000, 99999),
$content
);
$story->setTagsFromArray(["aaa", "bbb", "CCC"]);
echo "CREATING STORY..." . PHP_EOL;
$storyCreated = $storyApi->create($story)->data();
echo "Story created, ID: " . $storyCreated->id() . PHP_EOL;
echo " UUID: " . $storyCreated->uuid() . PHP_EOL;
echo " SLUG: " . $storyCreated->slug() . PHP_EOL;
$client = new ManagementApiClient("yourpersonalaccesstoken");
$spaceId = "yourspaceid";
$storyApi = new StoryApi($client, $spaceId);
// Setting up the hero-section
$heroSection = new StoryComponent("hero-section");
$heroSection->set("headline", "Hello World");
// We are going to setup an external image as background
$heroSection->setAsset("background_image", Asset::emptyAsset()->setExternalUrl("https://images.pexels.com/photos/18853169/pexels-photo-18853169/free-photo-of-tower-old-north-church-mirroring-in-puddle.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2"));
$heroSection->setAsset("background_video", Asset::emptyAsset());
$heroSection->set("text_color", "light");
$heroSection->set("vertical_alignment", "center");
$heroSection->set("horizontal_alignment", "center");
// Setting up the Button
$button = new StoryComponent("button");
$button->set("label", "Click here");
$button->set("style", "default");
$button->set("background_color", "primary");
$button->set("text_color", "light");
$button->set("size", "small");
$button->set("border_radius", "small");
// Setting up the Image Text Section
$imageTextSection = new StoryComponent("image-text-section");
$imageTextSection->set("headline", "Hello World");
$imageTextSection->setAsset("image", Asset::emptyAsset());
// Adding the Button to the Image Text Section (field name `button`)
$imageTextSection->addBlock("button", $button);
// Let's create the content type `default-page`
$page = new StoryComponent("default-page");
// Adding the Hero Section and the Image Text Section to the `body` field
$page->addBlock("body", $heroSection);
$page->addBlock("body", $imageTextSection);
$random = random_int(1_000_000, 9_999_999);
$story = new Story(
"Landing " . $random,
"landing-" . $random,
$page
);
$storyCreated = $storyApi->create($story)->data();
echo "Story created with ID : " . $storyCreated->id();
echo PHP_EOL;
$storyCreated->dump();
echo PHP_EOL;
// If we want to publish the story immediately ...
$storyPublished = $storyApi->publish($storyCreated->id())->data();
echo "Story published with ID : " . $storyPublished->id();
echo PHP_EOL;
echo "Story published at : " . $storyPublished->publishedAt();
echo PHP_EOL;
use Storyblok\ManagementApi\Endpoints\WorkflowApi;
// Define the tag details
$tag = [
"name" => "new tag",
"object_type" => "asset"
];
// Send the POST request to create the tag
$response = $managementApi()->post(
"spaces/{$spaceId}/internal_tags",
["internal_tag" => $tag]
);
// Show the URL of the response
echo $response->getLastCalledUrl() . PHP_EOL;
if ($response->isOk()) {
// Parse the created tag data
$createdTag = $response->data()->get("internal_tag");
echo "Tag created with id: " . $createdTag->get("id") . PHP_EOL;
echo $createdTag->toJson();
} else {
// Handle errors
echo $response->getErrorMessage();
}