2017 © Pedro Peláez
 

library contentful-management

SDK for the Contentful Content Management API

image

contentful/contentful-management

SDK for the Contentful Content Management API

  • Wednesday, May 16, 2018
  • by dborsatto
  • Repository
  • 27 Watchers
  • 4 Stars
  • 5,810 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 11 Versions
  • 42 % Grown

The README.md

contentful-management.php

Packagist PHP version Packagist CircleCI, (*1)

PHP SDK for Contentful's Content Management API. The SDK requires at least PHP 7.2 or PHP 8.0 and up., (*2)

Setup

Add this package to your application by using Composer and executing the following command:, (*3)

``` bash composer require contentful/contentful-management, (*4)


Then, if you haven't already, include the Composer autoloader: ``` php require_once 'vendor/autoload.php';

Basic concepts

The first thing that needs to be done is initiating an instance of Contentful\Management\Client by giving it an access token. All actions performed using this instance of the Client will be performed with the privileges of the user this token belongs to., (*5)

``` php $client = new \Contentful\Management\Client('access-token');, (*6)


When working with space-scoped or environment-scoped resources, you can use proxies. They are lazy-references to a space or an environment, and they allow you to avoid repeating the space and environment ID when making API calls: ``` php // Without space proxy $deliveryApiKeys = $client->getDeliveryApiKeys($spaceId); $roles = $client->getRoles($spaceId); // With space proxy $spaceProxy = $client->getSpaceProxy($spaceId); $deliveryApiKeys = $spaceProxy->getDeliveryApiKeys(); $roles = $spaceProxy->getRoles(); // Without environment proxy $assets = $client->getAssets($spaceId, $environmentId); $entries = $client->getEntries($spaceId, $environmentId); // With environment proxy $environmentProxy = $client->getEnvironmentProxy($spaceId, $environmentId); $assets = $environmentProxy->getAssets(); $entries = $environmentProxy->getEntries();

Usage

Api Keys

Fetching:, (*7)

``` php $deliveryApiKeys = $spaceProxy->getDeliveryApiKeys(); $deliveryApiKey = $spaceProxy->getDeliveryApiKey($deliveryApiKeyId);, (*8)

echo $deliveryApiKey->getSystemProperties()->getId(); echo $deliveryApiKey->getName(); echo $deliveryApiKey->getAccessToken(); $previewApiKeyLink = $deliveryApiKey->getPreviewApiKey();, (*9)

$previewApiKey = $spaceProxy->resolveLink($previewApiKeyLink); echo $previewApiKey->getAccessToken();, (*10)


Creating and modifying: ``` php $deliveryApiKey = new \Contentful\Management\Resource\DeliveryApiKey('Mobile'); $spaceProxy->create($deliveryApiKey); echo $deliveryApiKey->getSystemProperties()->getId(); echo $deliveryApiKey->getAccessToken(); $deliveryApiKey->delete();

Assets

Fetching:, (*11)

``` php $assets = $environmentProxy->getAssets(); // Optionally, pass a query object $query = (new \Contentful\Management\Query()) ->setLimit(5); $assets = $environmentProxy->getAssets($query);, (*12)

$asset = $environmentProxy->getAsset($assetId);, (*13)

echo $asset->getSystemProperties()->getId(); echo $asset->getTitle('en-US');, (*14)


Creating and modifying: ``` php $asset = new \Contentful\Management\Resource\Asset(); $file = new \Contentful\Core\File\RemoteUploadFile('Contentful.svg', 'image/svg+xml', $url); $asset->setTitle('en-US', 'My asset') ->setDescription('en-US', 'My description') ->setFile('en-US', $file); $environmentProxy->create($asset); // Omit the locale to process the files for all locales $asset->process('en-US'); $asset->setDescription('en-US', 'An even better description'); $asset->update(); $asset->archive(); $asset->unarchive(); $asset->publish(); $asset->unpublish(); $asset->delete();

Content types and content type snapshots

Fetching:, (*15)

``` php $contentTypes = $environmentProxy->getContentTypes(); // Optionally, pass a query object $query = (new \Contentful\Management\Query()) ->setLimit(5); $contentTypes = $environmentProxy->getContentTypes($query);, (*16)

$contentType = $environmentProxy->getContentType($contentTypeId);, (*17)

echo $contentType->getSystemProperties()->getId(); echo $contentType->getName();, (*18)

// Fetch the published version of content types $contentTypes = $environmentProxy->getPublishedContentTypes($query); $contentType = $environmentProxy->getPublishedContentType($contentTypeId);, (*19)

// Fetch snapshots from a content type, or from an environment proxy $snapshots = $contentType->getSnapshots(); $snapshot = $contentTy->getSnapshot($snapshotId);, (*20)

$snapshots = $environmentProxy->getContentTypeSnapshots($contentTypeId); $snapshot = $environmentProxy->getContentTypeSnapshot($contentTypeId, $snapshotId);, (*21)


Creating and modifying: ``` php $contentType = new \Contentful\Management\Resource\ContentType('Blog Post'); $contentType->setDescription('My description'); $contentType->addNewField('Symbol', 'title', 'Title'); $contentType->addNewField('Text', 'body', 'Body'); $customContentTypeId = 'blogPost'; $environmentProxy->create($contentType, $customContentTypeId); $contentType->addNewField('Date', 'publishedAt', 'Published At'); $contentType->update(); $contentType->publish(); $contentType->unpublish(); $contentType->delete();

Editor interfaces

Fetching and updating, (*22)

``` php $editorInterface = $environmentProxy->getEditorInterface($contentTypeId);, (*23)

$control = $editorInterface->getControl('website'); $control->setWidgetId('urlEditor');, (*24)

$editorInterface->update();, (*25)


### Entries and entry snapshots Fetching: ``` php $entries = $environmentProxy->getEntries(); // Optionally, pass a query object $query = (new \Contentful\Management\Query()) ->setLimit(5); $entries = $environmentProxy->getEntries($query); $entry = $environmentProxy->getEntry($entryId); echo $entry->getSystemProperties()->getId(); echo $entry->getField('title', 'en-US'); // Fetch snapshots from an entry, or from an environment proxy $snapshots = $entry->getSnapshots(); $snapshot = $entry->getSnapshot($snapshotId); $snapshots = $environmentProxy->getEntrySnapshots($contentTypeId); $snapshot = $environmentProxy->getEntrySnapshot($entryId, $snapshotId);

Creating and modifying:, (*26)

``` php $entry = new \Contentful\Management\Resource\Entry($contentTypeId); $entry->setField('title', 'en-US', 'My awesome blog post'); $entry->setField('body', 'en-US', 'Something something...');, (*27)

//Add existing assets $images = [ new \Contentful\Core\Api\Link('Example-existing-asset-id', 'Asset'), new \Contentful\Core\Api\Link('Example-existing-asset-id-2', 'Asset'), ]; $entry->setField('productImages', 'en-US', $images);, (*28)

$environmentProxy->create($entry);, (*29)

$entry->setField('body', 'en-US', 'Updated body'); $entry->update();, (*30)

$entry->archive(); $entry->unarchive();, (*31)

$entry->publish(); $entry->unpublish();, (*32)

$entry->delete();, (*33)


### Environments Fetching: ``` php $environments = $spaceProxy->getEnvironments(); // Optionally, pass a query object $query = (new \Contentful\Management\Query()) ->setLimit(5); $environments = $spaceProxy->getEnvironments($query); $environment = $spaceProxy->getEnvironment($environmentId); echo $environment->getSystemProperties()->getId(); echo $environment->getName();

Creating and modifying:, (*34)

``` php $environment = new \Contentful\Management\Resource\Environment('QA'); $spaceProxy->create($environment);, (*35)

$environmentId = $environment->getSystemProperties()->getId();, (*36)

// An environment might take a while to create, // depending on the size of the master environment, // so it might be a good idea to poll it until it's ready. do { $environment = $spaceProxy->getEnvironment($environmentId); $status = $environment->getSystemProperties()->getStatus()->getId(); } while ($status !== 'ready');, (*37)

$environment->delete();, (*38)


Creating an environment with a different source: ``` php $environment = new \Contentful\Management\Resource\Environment('QA','source-env-id'); $spaceProxy->create($environment); // An environment might take a while to create, // depending on the size of the master environment, // so it might be a good idea to poll it until it's ready. do { $environment = $spaceProxy->getEnvironment($environmentId); $status = $environment->getSystemProperties()->getStatus()->getId(); } while ($status !== 'ready'); $environment->delete();

Locales

Fetching:, (*39)

``` php $locales = $environmentProxy->getLocales(); // Optionally, pass a query object $query = (new \Contentful\Management\Query()) ->setLimit(5); $locales = $environmentProxy->getLocales($query);, (*40)

$locale = $environmentProxy->getLocale($localeId);, (*41)

echo $locale->getSystemProperties()->getId(); echo $locale->getName(); echo $locale->getCode();, (*42)


Creating and modifying: ``` php $locale = new \Contentful\Management\Resource\Locale('English (United States)', 'en-US'); $environmentProxy->create($locale); $locale->delete();

Organizations

Fetching:, (*43)

``` php $organizations = $client->getOrganizations(); $organization = $organizations[0];, (*44)

echo $organization->getSystemProperties()->getId(); echo $organization->getName();, (*45)


### Personal access tokens Fetching: ``` php $personalAccessTokens = $client->getPersonalAccessTokens(); // Optionally, pass a query object $personalAccessTokens = (new \Contentful\Management\Query()) ->setLimit(5); $personalAccessTokens = $client->getPersonalAccessTokens($query); $personalAccessToken = $client->getPersonalAccessToken($personalAccessTokenId); echo $personalAccessToken->getSystemProperties()->getId(); echo $personalAccessToken->getName();

Creating and modifying:, (*46)

``` php $readOnly = false; $personalAccessToken = new \Contentful\Management\Resource\PersonalAccessToken('Development access token', $readOnly); $client->create($personalAccessToken);, (*47)

// For security reasons, the actual token will only be available after creation. echo $personalAccessToken->getToken();, (*48)

$personalAccessToken->revoke();, (*49)


### Roles Fetching: ``` php $roles = $spaceProxy->getRoles(); // Optionally, pass a query object $query = (new \Contentful\Management\Query()) ->setLimit(5); $roles = $spaceProxy->getRoles($query); $role = $spaceProxy->getRole($roleId); echo $role->getSystemProperties()->getId(); echo $role->getName();

Creating and modifying:, (*50)

``` php $role = new \Contentful\Management\Resource\Role('Publisher');, (*51)

$policy = new \Contentful\Management\Resource\Policy('allow', 'publish'); $role->addPolicy($policy);, (*52)

$constraint = new \Contentful\Management\Resource\Role\Constraint\AndConstraint([ new \Contentful\Management\Resource\Role\Constraint\EqualityConstraint('sys.type', 'Entry'), new \Contentful\Management\Resource\Role\Constraint\EqualityConstraint('sys.type', 'Asset'), ]); $policy->setConstraint($constraint);, (*53)

$spaceProxy->create($role);, (*54)

$policy->delete();, (*55)


### Spaces Fetching: ``` php $spaces = $client->getSpaces(); // Optionally, pass a query object $query = (new \Contentful\Management\Query()) ->setLimit(5); $spaces = $client->getSpaces($query); $space = $client->getSpace($spaceId); echo $space->getSystemProperties()->getId(); echo $space->getName();

Creating and modifying:, (*56)

``` php $space = new \Contentful\Management\Resource\Space('Website', $organizationId, $defaultLocaleCode); $client->create($space);, (*57)

$space->delete();, (*58)


### Space memberships Fetching: ``` php $spaceMemberships = $spaceProxy->getSpaceMemberships(); // Optionally, pass a query object $query = (new \Contentful\Management\Query()) ->setLimit(5); $spaceMemberships = $spaceProxy->getSpaceMemberships($query); $spaceMembership = $spaceProxy->getSpaceMembership($spaceMembershipId); echo $spaceMembership->getSystemProperties()->getId(); echo $spaceMembership->getUser()->getId();

Creating and modifying:, (*59)

``` php $spaceMembership = new \Contentful\Management\Resource\SpaceMembership(); $spaceMembership->setEmail($userEmail) ->setAdmin(false) ->addRoleLink($roleId); $spaceProxy->create($spaceMembership);, (*60)

$spaceMembership->delete();, (*61)


### Uploads Fetching: ``` php $upload = $spaceProxy->getUpload($uploadId); echo $upload->getSystemProperties()->getId();

Creating and modifying:, (*62)

``` php // You can pass as argument an fopen resource, an actual string, or a PSR-7 compatible stream $upload = new \Contentful\Management\Resource\Upload(\fopen($myFile, 'r')); $spaceProxy->create($upload);, (*63)

$asset = new \Contentful\Management\Resource\Asset(); // To use the upload as an asset, you need to supply an asset name and a mime type $asset->setFile('en-US', $upload->asAssetFile('my-asset-name.png', 'image/png'));, (*64)

$environmentProxy->create($asset);, (*65)

$asset->process();, (*66)

$upload->delete();, (*67)


### UI extensions Fetching: ``` php $extensions = $environmentProxy->getExtensions(); // Optionally, pass a query object $query = (new \Contentful\Management\Query()) ->setLimit(5); $extensions = $environmentProxy->getExtensions($query); $extension = $environmentProxy->getExtension($extensionId); echo $extension->getSystemProperties()->getId(); echo $extension->getName();

Creating and modifying:, (*68)

``` php $extension = new \Contentful\Management\Resource\Extension('My awesome extension'); $extension->setSource('https://www.example.com/extension-source') ->addNewFieldType('Symbol');, (*69)

$environmentProxy->create($extension);, (*70)

$extension->addNewFieldType('Link', ['Entry']); $extension->update();, (*71)

$extension->delete();, (*72)


### Users Fetching: ``` php $user = $client->getUserMe(); echo $user->getSystemProperties()->getId(); echo $user->getEmail();

Webhooks

Fetching:, (*73)

``` php $webhooks = $spaceProxy->getWebhooks(); // Optionally, pass a query object $query = (new \Contentful\Management\Query()) ->setLimit(5); $webhooks = $spaceProxy->getWebhooks($query);, (*74)

$webhook = $spaceProxy->getWebhook($webhookId);, (*75)

echo $webhook->getSystemProperties()->getId(); echo $webhook->getName();, (*76)

// You can get calls and health from a webhook or from a space proxy $calls = $webhook->getCalls(); $call = $webhook->getCall($callId); $health = $webhook->getHealth();, (*77)

$calls = $spaceProxy->getWebhookCalls($webhookId); $call = $spaceProxy->getWebhookCall($webhookId, $callId); $health = $spaceProxy->getWebhookHealth($webhookId);, (*78)

echo $call->getStatusCode(); echo $call->getUrl(); echo $call->getEventType();, (*79)

echo $health->getTotal(); echo $health->getHealthy();, (*80)


Creating and modifying: ``` php $webhook = new \Contentful\Management\Resource\Webhook('Publish webhook', $url, ['Entry.publish']); $spaceProxy->create($webhook); $webhook->addTopic('Asset.publish'); $webhook->update(); $webhook->delete();

Rate limits and retrying

Some API calls are subject to rate limiting as described here. The SDK can be instructed to retry a call for a number of times via the max_rate_limit_retries option:, (*81)

php $client = new \Contentful\Management\Client('KEY',['max_rate_limit_retries' => 2]); $proxy = $client->getSpaceProxy('SPACE_ID'); $envName = uniqid(); $env = new \Contentful\Management\Resource\Environment($envName); $proxy->create($env); //this call will retry two times (so three calls couting the original one), before throwing an exception, (*82)

If the retry should happen in more than 60 seconds (as defined by the X-Contentful-RateLimit-Second-Remaining header here ), the call will throw a RateWaitTooLongException exception. This was implemented so that your scripts do not run for too long., (*83)

Contributinng

PRs are welcome! If you want to develop locally, however, you will need to install with --ignore-platform-reqs, as one of the libraries used for testing does currently not officially support PHP8., (*84)

About Contentful

Contentful is a content management platform for web applications, mobile apps and connected devices. It allows you to create, edit & manage content in the cloud and publish it anywhere via powerful API. Contentful offers tools for managing editorial teams and enabling cooperation between organizations., (*85)

License

Copyright (c) 2015-2019 Contentful GmbH. Code released under the MIT license. See LICENSE for further details., (*86)

The Versions

16/05 2018
16/05 2018

dev-feat/api-keys-environments

dev-feat/api-keys-environments

SDK for the Contentful Content Management API

  Sources   Download

MIT

The Requires

 

The Development Requires

18/04 2018

dev-chore/misc-release

dev-chore/misc-release

SDK for the Contentful Content Management API

  Sources   Download

MIT

The Requires

 

The Development Requires

26/03 2018
01/03 2018

dev-chore/move-namespace

dev-chore/move-namespace

SDK for the Contentful Content Management API

  Sources   Download

MIT

The Requires

 

The Development Requires

01/03 2018

dev-feat/core-package

dev-feat/core-package

SDK for the Contentful Content Management API

  Sources   Download

MIT

The Requires

 

The Development Requires

26/09 2017

dev-feature/code-generation

dev-feature/code-generation

SDK for the Contentful Content Management API

  Sources   Download

MIT

The Requires

 

The Development Requires

19/06 2017

dev-testing/locale-with-id

dev-testing/locale-with-id

SDK for the Contentful Content Management API

  Sources   Download

MIT

The Requires

 

The Development Requires