Installation
- Composer (Recommended)
- Manual Installation
composer require buttercms/buttercms-php
require_once('vendor/autoload.php');
Download the latest release and include it directly:
require_once('/path/to/buttercms-php/src/ButterCMS.php');
Requirements: PHP 5.3.0 or later.
Initialization
Initialize the SDK with your Read API token from the ButterCMS Settings.use ButterCMS\ButterCMS;
$butterCms = new ButterCMS('your_read_api_token');
API methods
Pages
Retrieve and search pages created in the ButterCMS dashboard.// Fetch a single page
$page = $butterCms->fetchPage('landing_page', 'home-page', [
'locale' => 'en',
'preview' => 1 // Include draft content
]);
// Access page fields
echo $page->getFields()['headline'];
echo $page->getField('body');
// Fetch paginated pages
$pages = $butterCms->fetchPages('landing_page', [
'page' => 1,
'page_size' => 10,
'locale' => 'en'
]);
// Access pages array
foreach ($pages->getPages() as $page) {
echo $page->getField('headline');
}
// Access pagination metadata
$meta = $pages->getMeta();
echo $meta['next_page'];
Collections
Fetch content from Collections (structured data tables).// Fetch collections
$collections = $butterCms->fetchCollections(['faq', 'navigation'], [
'locale' => 'en'
]);
// Access collection data
$faqItems = $collections->getCollection('faq');
foreach ($faqItems->getItems() as $item) {
echo $item['question'];
echo $item['answer'];
}
// Alternative: Content Fields (legacy method)
$content = $butterCms->fetchContentFields(['faq'], [
'locale' => 'en'
]);
Blog Posts
Access the built-in Blog Engine for posts, categories, tags, and authors.// List blog posts
$response = $butterCms->fetchPosts([
'page' => 1,
'page_size' => 10,
'exclude_body' => true // Exclude body for faster response
]);
// Access posts
foreach ($response->getPosts() as $post) {
echo $post->getTitle();
echo $post->getSlug();
}
// Access pagination
$meta = $response->getMeta();
echo $meta['next_page'];
// Fetch a single post
$post = $butterCms->fetchPost('my-post-slug');
echo $post->getTitle();
echo $post->getBody();
// Search posts
$results = $butterCms->searchPosts('search query', [
'page' => 1,
'page_size' => 10
]);
Authors
// List all authors
$response = $butterCms->fetchAuthors([
'include' => 'recent_posts' // Include recent posts
]);
foreach ($response->getAuthors() as $author) {
echo $author->getFirstName();
echo $author->getLastName();
}
// Fetch a single author
$author = $butterCms->fetchAuthor('jennifer-smith', [
'include' => 'recent_posts'
]);
Categories
// List all categories
$response = $butterCms->fetchCategories([
'include' => 'recent_posts'
]);
foreach ($response->getCategories() as $category) {
echo $category->getName();
echo $category->getSlug();
}
// Fetch a single category
$category = $butterCms->fetchCategory('news');
Tags
// List all tags
$response = $butterCms->fetchTags();
foreach ($response->getTags() as $tag) {
echo $tag->getName();
echo $tag->getSlug();
}
// Fetch a single tag
$tag = $butterCms->fetchTag('featured');
Feeds
Retrieve RSS, Atom, and Sitemap feeds.// Get RSS feed
$rss = $butterCms->fetchFeed('rss');
// Get Atom feed
$atom = $butterCms->fetchFeed('atom');
// Get Sitemap
$sitemap = $butterCms->fetchFeed('sitemap');
Query parameters reference
| Parameter | Applies To | Description |
|---|---|---|
page | Posts, Pages | Page number for pagination |
page_size | Posts, Pages | Number of items per page |
exclude_body | Posts | Exclude post body for faster response |
author_slug | Posts | Filter posts by author |
category_slug | Posts | Filter posts by category |
tag_slug | Posts | Filter posts by tag |
include | Authors, Categories, Tags | Include recent_posts with response |
locale | Pages, Collections | Locale code for localized content |
preview | Pages | Set to 1 to include draft content |
levels | Pages | Depth of nested references (1–3) |
Complete example
<?php
require_once('vendor/autoload.php');
use ButterCMS\ButterCMS;
$butterCms = new ButterCMS(getenv('BUTTERCMS_API_KEY'));
function fetchHomepageData($butterCms) {
try {
// Fetch the home page
$page = $butterCms->fetchPage('landing_page', 'home', [
'locale' => 'en'
]);
// Fetch navigation from collection
$collections = $butterCms->fetchCollections(['main_navigation']);
$navigation = $collections->getCollection('main_navigation');
// Fetch recent blog posts
$postsResponse = $butterCms->fetchPosts([
'page' => 1,
'page_size' => 3,
'exclude_body' => true
]);
return [
'page' => $page,
'navigation' => $navigation->getItems(),
'recentPosts' => $postsResponse->getPosts()
];
} catch (\Exception $e) {
error_log("Error fetching content: " . $e->getMessage());
return null;
}
}
// Usage
$data = fetchHomepageData($butterCms);
if ($data) {
echo "Page headline: " . $data['page']->getField('headline') . "\n";
echo "Navigation items: " . count($data['navigation']) . "\n";
echo "Recent posts: " . count($data['recentPosts']) . "\n";
}
Resources
GitHub Repository
View source code, report issues, and contribute
Packagist
Package details and version history