Was this page helpful?

Complete reference guide to the Delivery client library

Complete reference guide to the Delivery client library

The first thing that needs to be done is initiating an instance of Contentful\Delivery\Client by giving it three parameters: an access token, a space ID, and an environment ID (which is optional and defaults to master).

$client = new \Contentful\Delivery\Client($accessToken, $spaceId, $environmentId);

The client constructor also takes other optional parameters. This is the complete configuration:

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

Usage

Assets

Name Fetch one Fetch collection
Asset $client->getAsset($assetId, $locale = null) $client->getAssets($query = null)
File $asset->getFile() -
$asset = $client->getAsset($assetId, 'en-US');
echo $asset->getTitle('en-US');
echo $asset->getDescription('en-US');
/** @var Contentful\Core\File\FileInterface $file */
$file = $asset->getFile('en-US');

$assets = $client->getAssets();
foreach ($assets as $asset) {
    echo $asset->getTitle('en-US')
}

Content types

Name Fetch one Fetch collection
ContentType $client->getContentType($contentTypeId) $client->getContentTypes($query = null)
$contentType = $client->getContentType($contentTypeId);
echo $contentType->getName('en-US');

$contentTypes = $client->getContentTypes();
foreach ($contentTypes as $contentType) {
    echo $contentType->getName('en-US');
}

Entries

Name Fetch one Fetch collection
Entry $client->getEntry($assetId, $locale = null) $client->getEntries($query = null)
$entry = $client->getEntry($assetId, 'en-US');
// Entries have magic getters which correspond to field names
// This assumes you have a field named "title"
echo $entry->getTitle('en-US');

$entries = $client->getEntries();
foreach ($entries as $entry) {
    echo $entry->getTitle('en-US')
}

Environment and locales

Name Fetch one Fetch collection
Environment $client->getEnvironment() -
Locale - $environment->getLocales()

Environments in this client library are a virtual resource, which is used as a container for locales:

$environment = $client->getEnvironment();

foreach ($environment->getLocales() as $locale) {
    /** @var \Contentful\Delivery\Resource\Locale $locale */
    echo $locale->getCode();
}

Space

Name Fetch one Fetch collection
Space $client->getSpace() -
$space = $client->getSpace();
echo $space->getName();