Getting started with Contentful and PHP

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 an user's location by using our global CDN.

We publish SDKs for various languages to make developing applications easier. This article details how to get content using the PHP CDA SDK.

The Contentful PHP SDK is in beta and the API may change before a stable release.

Pre-requisites

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

Authentication

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.

Installation

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

1
php composer.phar install contentful/contentful:@beta

Add the Composer autoloader to the top of your project:

1
2
<?php
require_once 'vendor/autoload.php';

Setting up the Contentful client

Once you have installed the SDK you need to create a Client, using the space ID and authentication token you created above.

For this tutorial we'll use an example space

1
2
<?php
$client = new \Contentful\Delivery\Client('b4c0n73n7fu1', 'cfexampleapi');

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.

In this section we'll address entries and cover assets later on.

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

The code below retrieves all entries in your space from the API:

1
2
<?php
$entries = $client->getEntries();

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

1
2
3
<?php
$entryId = 'nyancat';
$entry = $client->getEntry($entryId);

To specify more complex queries you can use the query builder:

1
2
3
4
5
<?php
$query = new \Contentful\Delivery\Query;
$query->setContentType('cat')
      ->orderBy('sys.createdAt');
$catEntriesByDate = $client->getEntries($query);

Using your entry

Once you've got your entry, you can access the content it holds through getter methods:

1
2
3
<?php
echo $cat->getName(); // "Nyan Cat"
echo "I have $cat->getLives() lives"; // "I have 1337 lives"

If an entry contains a link to an asset or another entry, it will automatically be loaded when accessing it:

1
2
<?php
echo $cat->getBestFriend()->getName(); // "Happy Cat"

Using assets

Querying assets works like querying entries.

You can retrieve all assets of your space:

1
2
<?php
$assets = $client->getAssets();

Or you can get a single asset:

1
2
3
<?php
$assetId = 'nyancat';
$asset = $client->getAsset($assetId);

As with entries you can also use more complex queries:

1
2
3
4
<?php
$query = new \Contentful\Delivery\Query;
$query->orderBy('sys.createdAt');
$assets = $client->getAssets($query);

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

1
2
3
<?php
echo $asset->getName(); // "Nyan Cat"
echo $asset->getFile()->getUrl(); // "//images.contentful.com/cfexampleapi/4gp6taAwW4CmSgumq2ekUm/9da0cd1936871b8d72343e895a00d611/Nyan_cat_250px_frame.png"

Using the Images API you can control details 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:

1
2
3
4
5
<?php
$options = new \Contentful\Delivery\ImageOptions;
$options->setFormat('jpg')
        ->setHeight(100);
$url = $asset->getName()->getFile()->getUrl($options);

Next steps