Contentful - Example code for the Content Management API

1.7k views Asked by At

Contentful has only a PHP SDK for the Content Delivery API (ready only). I need to update a entry from my PHP code and I could not find any sample code in the documentation.

Im trying to use the REST API directly (https://www.contentful.com/developers/docs/references/content-management-api/#/reference/entries/entries-collection/create-an-entry), but having tons of problems to send the correct form data with fields.

Does anyone have a sample code of how I can update the title of a entry using the entry ID?

thanks

1

There are 1 answers

5
Robban On

To update an entry in Contentful you need to supply a valid JSON payload with your request. Now this of course depends on how your specific entry and content type looks like but here's a working example of such a payload:

{
 "fields": {
    "title": {
       "en-US": "Hello, World!"
          },
     "body": {
        "en-US": "Bacon is healthy!"
          }
     }
}

Note that you need to nest each field value with the locales that you wish to update.

As the Contentful API uses optimistic concurrency you also need to supply a header with the latest known version of the entry. For example:

X-Contentful-Version:4

This payload with the correct version header needs to be sent with a PUT request to https://api.contentful.com/spaces/<space_id>/entries/<entry_id>

Replace the <space_id> and <entry_id> with your specific values.

Note that this is different from the Content Delivery Endpoints which live at https://cdn.contentful.com

Also keep in mind that to access the management endpoints you need a management access token. A delivery access token will not do. For more information about how to create and use a management token refer to the official documentation here: https://www.contentful.com/developers/docs/references/authentication/#the-content-management-api