Complete reference guide to the Delivery SDK

Complete reference guide to the Delivery SDK

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',
    // Set to true to use the Preview API instead of the default Delivery API
    $preview = true,
    // Defaults to null, this will be the default locale used in queries
    $defaultLocale = 'en-US',
    $options = [
      // An instance of GuzzleHttp\Client
      'guzzle' => $guzzle,
      // A PSR-3 logger
      'logger' => $logger,
      // You can use this option if you're using some sort of proxy
      'baseUri' => 'https://cdn.contentful.com',
      // A PSR-6 cache pool
      'cache' => $cache,
      // If set to true, resources that can be cached will be stored in the PSR-6 pool at runtime
      'autoWarmup' => true,
    ]
);

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 SDK 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();