The Content Management client library for PHP
This guide will show you how to get started using our PHP Content Management client library to manage content.
Installation
The easiest way to install the Contentful PHP client library is to use Composer and run the following command:
If you haven’t already, require Composer’s autoload at the beginning of your application:
Setting up the Contentful client
Once you have installed the client library you need to instantiate a Client object. From that, retrieve an environment proxy: it’s a utility resource which wraps resources that are environment-bound, such as entries, assets, and content types.
Now we need to take a brief look at the concept of proxy. In the client library, a proxy is a lazy reference to a resource, and it’s designed to be used as the preferred way of working with resources that have a certain scope. There are two supported proxies: SpaceProxy, and EnvironmentProxy. The first one is supposed to be used to handle resources that are scoped to a space (API keys, roles, space memberships, file uploads, and webhooks), whereas the second is for handling resources that are scoped to an environment (assets, content types, content type snapshots, entries, entry snapshots, locales). In practice, this means that instead of relying on the client object and having to specify all required parameters on every operation, you can use a proxy and rely on that:
As you can see, an environment proxy resource is generally easier to pass around and use, as it encapsulates a reference to three things: a client, a space ID, and an environment ID. The space proxy works in the same way:
As you can see, proxies provide a more convenient way of working with resources; for this reason, we suggest using them whenever possible. In the rest of the article, we’ll be using $spaceProxy and $environmentProxy to refer to such objects.
Managing your content
Even though the client library supports all Content Management API endpoints, the main use case is programmatically handling content types, assets, and entries. This getting started tutorial will focus on these resources. For all other options, please refer to the complete reference guide to the Management client library.
Content types
Creating a content type
You’ll be able to access and work with fields using the following methods:
In the namespace Contentful\Management\Resource\ContentType\Field you will find all possible options for fields. They are a 1:1 representation of the types exposed by the Content Management API: you can take a look at the client library API docs to learn more.
Retrieving a content type
Updating a content type
Deleting a content type
Publishing a content type
In order for an entry to use a content type, you must first publish it:
Entries
Let’s go through the CRUD operations that you can do on entries. These examples assumes you have a content type whose ID is blogPost.
Creating an entry
Retrieving an entry
Updating an entry
Deleting an entry
More with entries
Besides CRUD options, Contentful allows you to handle publishing and archiving entries. It looks like this:
Assets
Assets are very similar to entries, in terms of which operations you can perform on them (CRUD, publish/unpublish, archive/unarchive). The main difference in terms of usage is that assets will always have the same three fields (title, description, file), which means that the client library provides methods for dealing directly with those fields. The only tricky part is how to handle file uploads. Let’s take a look:
In the previous example, we saw how it is possible to upload to Contentful a file that is already hosted somewhere. But it’s also possible to upload a file in the local filesystem. In order to do so, you need first to actually upload the file, then link the asset to it:
After you’re done creating an asset, you can perform the usual operations on it: