Attention! Version 3 of this package supports PHP 5.6, but starting from version 4, PHP 7 will be required.
PHP SDK 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.
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
- Content retrieval through the Content Delivery API and Content Preview API.
- Synchronization
- Localization support
- Link resolution
In order to get started with the Contentful PHP SDK 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.
Install the library using Composer:
composer require contentful/contentful
The following code snippet is the most basic one you can use to get some content from Contentful with this SDK:
All interactions with the SDK 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
}
This SDK 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:
$client = new \Contentful\Delivery\Client(
'b4c0n73n7fu1',
'cfexampleapi',
'master',
true // Set this to true to use the Preview API
);
You can find all available methods of our client in our reference documentation.
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.
The client constructor supports several options you may set to achieve the expected behavior:
$client = new \Contentful\Delivery\Client(
$accessToken,
$spaceId,
$environmentId = 'master',
$usePreview = false,
$defaultLocale = null,
$options = [
'baseUri' => $baseUri = null,
'guzzle' => $guzzle = null,
'logger' => $logger = null,
'cache' => $cache = null,
'autoWarmup' => $autoWarmup = false,
]
);
Name | Default | Description |
---|---|---|
$accessToken |
Required. Your access token | |
$spaceId |
Required. Your space ID | |
$environmentId |
'master' |
Your environment ID |
$usePreview |
false |
Set to true to use the Preview API instead of the default Delivery API |
$defaultLocale |
null |
Set a locale to be automatically used for all requests |
$baseUri |
null |
A string to override the default Contentful API URL (https://cdn.contentful.com for Delivery API, and https://preview.contentful.com for Preview API) |
$guzzle |
null |
A custom instance of a GuzzleHttp\Client object |
$logger |
null |
A PSR-3 logger which implements Psr\Log\LoggerInterface |
$cache |
null |
A PSR-6 cache item pool which implements Psr\Cache\CacheItemPoolInterface |
$autoWarmup |
false |
When using a cache pool, set this to true to automatically fill the cache during regular use |
The PHP SDK 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.
- 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.
For details about how to upgrade from version 2.x to version 3, please check the upgrade guide.
- File an issue here on GitHub: . Make sure to remove any credential from your code before sharing it.
This repository is published under the MIT license.
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.