contentful/contentful

SDK for the Contentful Content Delivery API


README

PHP

Join Contentful Community Slack   Join Contentful Community Forum

contentful.php — Contentful PHP Delivery Library

Packagist PHP version Packagist CircleCI

PHP library for the Contentful Content Delivery API and Content Preview API. It helps you to easily access your Content stored in Contentful with your PHP applications.

What is Contentful?

Contentful provides content infrastructure for digital teams to power websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management and delivery APIs, and a customizable web app that enable developers and content creators to ship their products faster.

Table of contents

Core Features

Getting started

In order to get started with the Contentful PHP library you'll need not only to install it, but also to get credentials which will allow you to have access to your content in Contentful. This package requires PHP 7.2 or higher or PHP 8.0 or higher.

Installation

Install the library using Composer:

composer require contentful/contentful

Your first request

The following code snippet is the most basic one you can use to get some content from Contentful with this library: All interactions with the library go through Contentful\Delivery\Client. To create a new client an access token and a space ID have to be passed to the constructor.

$client = new \Contentful\Delivery\Client(
    'b4c0n73n7fu1', # This is the access token for this space. Normally you get both ID and the token in the Contentful web app
    'cfexampleapi' # This is the space ID. A space is like a project folder in Contentful terms
);

try {
    $entry = $client->getEntry('nyancat');
} catch (\Contentful\Core\Exception\NotFoundException $exception) {
    // Entry does not exist
}

Using this library with the Preview API

This library can also be used with the Preview API. In order to do so, you need to use the Preview API access token, available on the same page where you get the Delivery API token, and tell the client to use the different API:

$options = \Contentful\Delivery\ClientOptions::create()
    ->usingPreviewApi();
$client = new \Contentful\Delivery\Client($accessToken, $spaceId, $environmentId, $options);

You can find all available methods of our client in our reference documentation.

Authentication

To get your own content from Contentful, an app should authenticate with an OAuth bearer token.

You can create API keys using the Contentful web interface. Go to the app, open the space that you want to access (top left corner lists all the spaces), and navigate to the APIs area. Open the API Keys section and create your first token. Done.

Don't forget to also get your Space ID.

For more information, check the Contentful REST API reference on Authentication.

Documentation & References

Configuration

The ClientOptions class allows you to configure the client in a variety of different ways:

$options = \Contentful\Delivery\ClientOptions::create()
    ->usingPreviewApi()
    ->withDefaultLocale(string $defaultLocale = null)
    ->withHost(string $host)
    ->withLogger(Psr\Log\LoggerInterface $logger)
    ->withCache(Psr\Cache\CacheItemPoolInterface $cache, bool $autoWarmup = false, bool $cacheContent = false)
    ->withHttpClient(GuzzleHttp\Client $client)
    ->withoutMessageLogging()
    ->withQueryCache(Psr\Cache\CacheItemPoolInterface $queryCacheItemPool, int $queryCacheLifetime = 0)
;

$client = new \Contentful\Delivery\Client(
    string $accessToken,
    string $spaceId,
    string $environmentId = 'master',
    ClientOptions $options = null
);

Reference documentation

The PHP library reference documents what objects and methods are exposed by this library, what arguments they expect and what kind of data is returned.

Most methods also have examples which show you how to use them.

Tutorials & other resources

  • This library is a wrapper around our Contentful Delivery REST API. Some more specific details such as search parameters and pagination are better explained on the REST API reference, and you can also get a better understanding of how the requests look under the hood.
  • Check the Contentful for PHP page for Tutorials, Demo Apps, and more information on using PHP with Contentful.

Upgrade

For details about how to upgrade from version 3.x to version 4, please check the changelog entry for version 4.0.0 and the upgrade to version 4 guide.

For details about how to upgrade from version 2.x to version 3, please check the changelog entry for version 3.0.0 and the upgrade to version 3 guide.

Reach out to us

You have questions about how to use this library?

  • Reach out to our community forum: Contentful Community Forum
  • Jump into our community slack channel: Contentful Community Slack

You found a bug or want to propose a feature?

  • File an issue here on GitHub: File an issue. Make sure to remove any credential from your code before sharing it.

You need to share confidential information or have other questions?

  • File a support ticket at our Contentful Customer Support: File support ticket

Get involved

PRs Welcome

Important: Right now, the API has php-vcr as a development dependency, which does not officially support PHP8 yet. If you want to develop on PHP8, you will need to install the dependencies with composer install --ignore-platform-reqs to overwrite this requirement.

License

This repository is published under the MIT license.

Code of Conduct

We want to provide a safe, inclusive, welcoming, and harassment-free space and experience for all participants, regardless of gender identity and expression, sexual orientation, disability, physical appearance, socioeconomic status, body size, ethnicity, nationality, level of experience, age, religion (or lack thereof), or other identity markers.

Read our full Code of Conduct.