1. Go to this page and download the library: Download sensiolabs-de/storyblok-api 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/ */
sensiolabs-de / storyblok-api example snippets
use SensioLabs\Storyblok\Api\StoryblokClient;
$client = new StoryblokClient(
baseUri: 'https://api.storyblok.com',
token: '***********',
timeout: 10 // optional
);
// you can now request any endpoint which needs authentication
$client->request('GET', '/api/something', $options);
use SensioLabs\Storyblok\Api\SpacesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
$client = new StoryblokClient(/* ... */);
$spacesApi = new SpacesApi($client);
$response = $spacesApi->me();
use SensioLabs\Storyblok\Api\StoriesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
$client = new StoryblokClient(/* ... */);
$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(locale: 'de');
use SensioLabs\Storyblok\Api\StoriesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\Domain\Value\Dto\Version;
$client = new StoryblokClient(/* ... */);
$storiesApi = new StoriesApi($client, Version::Draft);
$response = $storiesApi->bySlug(
locale: 'de',
slug: '/my-story/',
);
use SensioLabs\Storyblok\Api\StoriesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\Domain\Value\Dto\Version;
$client = new StoryblokClient(/* ... */);
$storiesApi = new StoriesApi($client, Version::Published);
$response = $storiesApi->bySlug(
locale: 'de',
slug: '/my-story/',
version: Version::Draft, // This overrides the global "version"
);
use SensioLabs\Storyblok\Api\StoriesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\Domain\Value\Dto\Pagination;
$client = new StoryblokClient(/* ... */);
$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(
locale: 'de',
pagination: new Pagination(page: 1, perPage: 30)
);
use SensioLabs\Storyblok\Api\StoriesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\Domain\Value\Dto\SortBy;
use SensioLabs\Storyblok\Api\Domain\Value\Dto\Direction;
$client = new StoryblokClient(/* ... */);
$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(
locale: 'de',
sortBy: new SortBy(field: 'title', direction: Direction::Desc)
);
use SensioLabs\Storyblok\Api\StoriesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\Domain\Value\Filter\FilterCollection;
use SensioLabs\Storyblok\Api\Domain\Value\Dto\Direction;
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\InFilter;
$client = new StoryblokClient(/* ... */);
$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(
locale: 'de',
filters: new FilterCollection([
new InFilter(field: 'single_reference_field', value: 'f2fdb571-a265-4d8a-b7c5-7050d23c2383')
])
);
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\AllInArrayFilter;
new AllInArrayFilter(field: 'tags', value: ['foo', 'bar', 'baz']);
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\AnyInArrayFilter;
new AnyInArrayFilter(field: 'tags', value: ['foo', 'bar', 'baz']);
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\GreaterThanDateFilter;
new GreaterThanDateFilter(field: 'created_at', value: new \DateTimeImmutable());
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\LessThanDateFilter;
new LessThanDateFilter(field: 'created_at', value: new \DateTimeImmutable());
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\GreaterThanFloatFilter;
new GreaterThanFloatFilter(field: 'price', value: 39.99);
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\LessThanFloatFilter;
new LessThanFloatFilter(field: 'price', value: 199.99);
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\GreaterThanIntFilter;
new GreaterThanIntFilter(field: 'stock', value: 0);
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\LessThanIntFilter;
new LessThanIntFilter(field: 'stock', value: 100);
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\InFilter;
new InFilter(field: 'text', value: 'Hello World!');
// or
new InFilter(field: 'text', value: ['Hello Symfony!', 'Hello SensioLabs!']);
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\NotInFilter;
new NotInFilter(field: 'text', value: 'Hello World!');
// or
new NotInFilter(field: 'text', value: ['Bye Symfony!', 'Bye SensioLabs!']);
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\IsFilter;
// You can use one of the following constants:
// IsFilter::EMPTY_ARRAY
// IsFilter::NOT_EMPTY_ARRAY
// IsFilter::EMPTY
// IsFilter::NOT_EMPTY
// IsFilter::TRUE
// IsFilter::FALSE
// IsFilter::NULL
// IsFilter::NOT_NULL
new IsFilter(field: 'text', value: IsFilter::EMPTY);
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\LikeFilter;
new LikeFilter(field: 'description', value: '*I love Symfony*');
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\NotLikeFilter;
new NotLikeFilter(field: 'description', value: '*Text*');
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\OrFilter;
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\LikeFilter;
use SensioLabs\Storyblok\Api\Domain\Value\Filter\Filters\NotLikeFilter;
new OrFilter(
new LikeFilter(field: 'text', value: 'Yes!*'),
new LikeFilter(field: 'text', value: 'Maybe!*'),
// ...
);
use SensioLabs\Storyblok\Api\StoriesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
$client = new StoryblokClient(/* ... */);
$storiesApi = new StoriesApi($client);
$response = $storiesApi->allByContentType('custom_content_type', locale: 'de');
use SensioLabs\Storyblok\Api\StoriesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\Domain\Value\Uuid;
$uuid = new Uuid(/** ... */);
$client = new StoryblokClient(/* ... */);
$storiesApi = new StoriesApi($client);
$response = $storiesApi->byUuid($uuid, locale: 'de');
use SensioLabs\Storyblok\Api\StoriesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
$client = new StoryblokClient(/* ... */);
$storiesApi = new StoriesApi($client);
$response = $storiesApi->bySlug('folder/slug', locale: 'de');
use SensioLabs\Storyblok\Api\StoriesApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\Domain\Value\Id;
$id = new Id(/** ... */);
$client = new StoryblokClient(/* ... */);
$storiesApi = new StoriesApi($client);
$response = $storiesApi->byId($id, locale: 'de');
use SensioLabs\Storyblok\Api\LinksApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
$client = new StoryblokClient(/* ... */);
$linksApi = new LinksApi($client);
$response = $linksApi->all();
use SensioLabs\Storyblok\Api\LinksApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\Domain\Value\Dto\Pagination;
$client = new StoryblokClient(/* ... */);
$linksApi = new LinksApi($client);
$response = $linksApi->all(
pagination: new Pagination(page: 1, perPage: 1000)
);
use SensioLabs\Storyblok\Api\LinksApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\Domain\Value\Id;
$id = new Id(/** ... */);
$client = new StoryblokClient(/* ... */);
$linksApi = new LinksApi($client);
$response = $linksApi->byParent($id);
use SensioLabs\Storyblok\Api\LinksApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
$client = new StoryblokClient(/* ... */);
$linksApi = new LinksApi($client);
$response = $linksApi->roots($id);
use SensioLabs\Storyblok\Api\DatasourceApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
$client = new StoryblokClient(/* ... */);
$api = new DatasourceApi($client);
$response = $api->byName('tags'); // returns SensioLabs\Storyblok\Api\Domain\Value\Datasource
use SensioLabs\Storyblok\Api\DatasourceApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\Domain\Value\Datasource\Dimension;
$client = new StoryblokClient(/* ... */);
$api = new DatasourceApi($client);
$response = $api->byName('tags', new Dimension('de')); // returns SensioLabs\Storyblok\Api\Domain\Value\Datasource
use SensioLabs\Storyblok\Api\TagsApi;
use SensioLabs\Storyblok\Api\StoryblokClient;
$client = new StoryblokClient(/* ... */);
$api = new TagsApi($client);
$response = $api->all(); // returns SensioLabs\Storyblok\Api\Response\TagsResponse
use SensioLabs\Storyblok\Api\StoryblokClient;
use SensioLabs\Storyblok\Api\AssetsApi;
$client = new StoryblokClient(
baseUri: 'https://api.storyblok.com',
token: 'assets-api-token',
timeout: 10 // optional
);
$assetsApi = new AssetsApi($assetsClient);
$assetsApi->get('filename.png')
declare(strict_types=1);
namespace App\ContentType;
use IteratorAggregate;
use SensioLabs\Storyblok\Api\Response\StoriesResponse;
/**
* @template T of ContentTypeInterface
*
* @implements IteratorAggregate<int, T>
*/
abstract readonly class ContentTypeCollection implements \Countable, \IteratorAggregate
{
public int $total;
public int $perPage;
public int $curPage;
public int $lastPage;
public ?int $prevPage;
public ?int $nextPage;
/**
* @var list<T>
*/
private array $items;
final public function __construct(StoriesResponse $response)
{
$this->items = array_values(array_map($this->createItem(...), $response->stories));
$this->total = $response->total->value;
$this->curPage = $response->pagination->page;
$this->perPage = $response->pagination->perPage;
$this->lastPage = (int) ceil($this->total / $this->perPage);
$this->prevPage = 1 < $this->curPage ? $this->curPage - 1 : null;
$this->nextPage = $this->curPage < $this->lastPage ? $this->curPage + 1 : null;
}
/**
* @return \Traversable<int, T>
*/
final public function getIterator(): \Traversable
{
return new \ArrayIterator($this->items);
}
final public function count(): int
{
return \count($this->items);
}
/**
* @param array<string, mixed> $values
*
* @return T
*/
abstract protected function createItem(array $values): ContentTypeInterface;
}
declare(strict_types=1);
namespace App\ContentType\BlogPost;
use App\ContentType\ContentTypeCollection;
use App\ContentType\ContentTypeFactory;
/**
* @extends ContentTypeCollection<BlogPost>
*/
final readonly class BlogPostCollection extends ContentTypeCollection
{
protected function createItem(array $values): BlogPost
{
return ContentTypeFactory::create($values, BlogPost::class);
}
}
new BlogPostCollection(
$this->stories->allByContentType(
BlogPost::type(),
new StoriesRequest(
language: $this->localeSwitcher->getLocale(),
pagination: new Pagination($this->curPage, self::PER_PAGE),
sortBy: new SortBy('first_published_at', Direction::Desc),
filters: $filters,
excludeFields: new FieldCollection([
new Field('body'),
new Field('additional_contents'),
]),
),
),
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.