Getting Started with Contentful and PHP

This guide will show you how to get started using our PHP SDK to consume content.

Contentful's Content Delivery API (CDA) is a read-only API for retrieving content from Contentful. All content, both JSON and binary, is fetched from the server closest to a user's location by using our global CDN.

We publish SDKs for various languages to make developing applications easier.


This tutorial assumes you have read and understood the guide that covers the Contentful data model.


For every request, clients need to provide an API key, which is created per space and used to delimit applications and content classes.

You can create an access token using the Contentful web app or the Content Management API.


The easiest way to install the Contentful PHP SDK is to use Composer and run the following command:

php composer.phar install contentful/contentful

Add the Composer autoloader to the top of your project:

require_once 'vendor/autoload.php';

Setting up the Contentful client

Once you have installed the SDK you need to create a Client.

Initializing the client

You need an API key and a space ID to initialize a client

You can use the API key and space ID pre-filled below from our example space or replace them with your own values.

$client = new \Contentful\Delivery\Client('<access_token>', '<space_id>');

Getting your content

Contentful separates content between entries, which contain your data and relationships with other content or images, and assets, which represent static content, like images, and are served as files. You can read more about this in our data model concepts guide.


With the client created, you can start consuming data from the API.

The code below retrieves all entries in your space from the API, but don't print the output yet, as this will result in a lot of JSON, you will learn how to filter the output later:

$entries = $client->getEntries();

Whereas this code retrieves a single entry specified by an ID.

$entryId = '<entry_id>';
$entry = $client->getEntry($entryId);
echo $entry->getproductName();
Playsam Streamliner Classic Car, Espresso

To specify more complex queries you can use the query builder. The example below filters results to a specific content type (the product) and sorts them by price:

$query = new \Contentful\Delivery\Query;

$productEntriesByPrice = $client->getEntries($query);

Using the entry

Once you've got the entry, you can access its content through getter methods:

foreach ($productEntriesByPrice as $product) {
    echo $product->getproductName(), PHP_EOL;
Whisk Beater
Playsam Streamliner Classic Car, Espresso
Hudson Wall Cup
SoSo Wall Clock

If an entry contains a link to an asset or another entry, the SDK will automatically load it. The example below shows the name of the brand linked to the product:

foreach ($productEntriesByPrice as $product) {
    echo $product->getproductName(), ', Brand: ', $product->getBrand()->getcompanyName(), PHP_EOL;

Using assets

Querying assets works similarly to querying entries. You can retrieve all assets from a space with the following:

$assets = $client->getAssets();

Or to get a single asset:

$assetId = '<asset_id>';
$asset = $client->getAsset($assetId);

As with entries you can also use more complex queries:

$query = new \Contentful\Delivery\Query;
$assets = $client->getAssets($query);

Once you have an asset, you can access its metadata and an URL for the actual file:

echo $asset->getTitle(), PHP_EOL;
echo $asset->getFile()->getUrl();

Using the Images API you can control how Contentful serves images. For example, to convert an image to a JPEG and resize it to a height of no more than 100 pixels:

$options = new \Contentful\File\ImageOptions;
echo $asset->getFile()->getUrl($options);