Skip to content
PHP API client for ButterCMS (
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/ButterCMS Adding page_type to Page Model Apr 10, 2019
.gitignore README Formatting Cleanup Nov 9, 2017
composer.json updates readme Jul 8, 2016

ButterCMS PHP API Wrapper

This wrapper enables PHP developers to quickly and easily get up and running with ButterCMS. It is based of off the API documentation.


PHP 5.3.0 and later.


You can install the bindings via Composer. Run the following command:

composer require buttercms/buttercms-php

To use the bindings, use Composer's autoload:


Manual Installation

If you do not wish to use Composer, you can download the latest release. Then, to use the bindings, include the src/ButterCMS.php file.



use ButterCMS\ButterCMS;

$butterCms = new ButterCMS('<auth_token>');

$page = $butterCms->fetchPage('about', 'welcome-to-the-site');

// These are equivalent
echo $page->getFields()['some-field'];
echo $page->getField('some-field');

$pagesResponse = $butterCms->fetchPages('news', ['breaking-news' => true]);
foreach ($pagesResponse->getPages() as $page) {
    echo $page->getSlug();
// Error Handling
try {
    $butterCms->fetchPage('about', 'non-existent-page');
} catch (GuzzleHttp\Exception\BadResponseException $e) {
    // Happens for any non-200 response from the API
} catch (\UnexpectedValueException $e) {
    // Happens if there is an issue parsing the JSON response

Content Fields

use ButterCMS\ButterCMS;

$butterCms = new ButterCMS('<auth_token>');

// Returns your fields turned in to a multidimensional array
$butterCms->fetchContentFields(['headline', 'FAQ'])

// Localization
$butterCms->fetchContentFields(['headline', 'FAQ'], ['locale' => 'en']);

// Test Mode
$butterCms->fetchContentFields(['headline', 'FAQ'], ['test' => '1']);

Blog Engine

use ButterCMS\ButterCMS;

$butterCms = new ButterCMS('<auth_token>');

// Posts
$result = $butterCms->fetchPosts(['page' => 1]);

$meta = $result->getMeta(); // Meta information like pagination

$posts = $result->getPosts(); // Get posts array off of result

$post = $posts[0]; // Get the first post
echo $post->getTitle(); // Access attributes using getXxxx() format.
echo $post->getSlug();

$author = $post->getAuthor(); // Access nested objects: Author, Tags, Categories like so
echo $author->getFirstName();
echo $author->getLastName();

// Loop through posts
foreach ($posts as $post) {
    echo $post->getTitle();

// Query for one post
$response = $butterCms->fetchPost('post-slug');
$post = $response->getPost();
echo $post->getTitle();

// Authors
$butterCms->fetchAuthors(['include' => 'recent_posts']);

// Categories
$butterCms->fetchCategories(['include' => 'recent_posts']);

// Tags

// Feeds - returns a SimpleXMLElement object
$feed = $butterCms->fetchFeed('rss');

// Search
$butterCms->searchPosts('query', ['page' => 1]);


View PHP Blog engine and Full CMS for other examples of using ButterCMS with PHP.

You can’t perform that action at this time.