This is the Storyblok php client for easy access of the publishing api.
The library checks the get parameters _storyblok to get the draft version of a specific story and _storyblok_published to clear the cache.
composer require storyblok/php-client dev-master
// Require composer autoload
require 'vendor/autoload.php';
// Initialize
$client = new \Storyblok\Client('your-storyblok-private-token');
// Optionally set a cache
$client->setCache('filesytem', array('path' => 'cache'));
// Get the story as array
$client->getStoryBySlug('home');
$data = $client->getBody();
// Require composer autoload
require 'vendor/autoload.php';
// Initialize
$client = new \Storyblok\Client('your-storyblok-private-token');
// Optionally set a cache
$client->setCache('filesytem', array('path' => 'cache'));
// Get all Stories that start with news
$client->getStories(
array(
'starts_with' => 'news'
)
);
$data = $client->getStoryContent();
// Require composer autoload
require 'vendor/autoload.php';
// Initialize
$client = new \Storyblok\Client('your-storyblok-private-token');
// Optionally set a cache
$client->setCache('filesytem', array('path' => 'cache'));
// Get all Stories that start with news
$client->getDatasourceEntries('categories');
// will return the whole response
$data = $client->getBody();
// will return as ['name']['value'] Array for easy access
$nameValueArray = $client->getAsNameValueArray();
// Require composer autoload
require 'vendor/autoload.php';
// Initialize
$client = new \Storyblok\Client('your-storyblok-private-token');
// Optionally set a cache
$client->setCache('filesytem', array('path' => 'cache'));
// Get all Tags
$client->getTags();
// will return the whole response
$data = $client->getBody();
// will return as ['tagName1', 'tagName2'] Array for easy access
$stringArray = $client->getAsStringArray();
// Require composer autoload
require 'vendor/autoload.php';
// Initialize
$client = new \Storyblok\Client('your-storyblok-private-token');
// Optionally set a cache
$client->setCache('filesytem', array('path' => 'cache'));
// Get all Tags
$client->getTags();
// Let's you acces the Headers
var_dump($client->getHeaders());
In order to flush the cache when the user clicks publish, you need to listen to the published event in javascript or define a webhook in the space settings that clears the cache on your server.
<script type="text/javascript" src="//app.storyblok.com/f/storyblok-latest.js"></script>
<script type="text/javascript">
storyblok.init()
storyblok.on('published', function() {
$.ajax({
url: '/clear.php'
})
})
</script>
In clear.php:
$client = new \Storyblok\Client('your-storyblok-private-token');
$client->setCache('filesytem', array('path' => 'cache'));
// Flush the whole cache when a story has been published
$client->flushCache();
// Or empty the cache for one specific item only
$client->deleteCacheBySlug('home');
$tree = $client->editMode()->getLinks()->getAsTree();
echo '<ul>';
foreach ($tree as $item) {
echo '<li>' . $item['item']['name'];
if (!empty($item['children'])) {
echo '<ul>';
foreach ($item['children'] as $item2) {
echo '<li>' . $item2['item']['name'] . '</li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
This project is open-sourced software licensed under the MIT license