2

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 Answer 1

2

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

5
  • Thanks. That is an example to create a new entry, correct? How can I set the entry ID of the object I want to update? Jan 3, 2017 at 21:20
  • 1
    Updating the answer to include the id.
    – Robban
    Jan 3, 2017 at 21:23
  • 1
    You can actually use the same endpoint to both create and update an entry. It will update any existing entry (provided you add the version header) or it will create a new one if the id does not exist. That way you can control the id of your created entries.
    – Robban
    Jan 3, 2017 at 21:34
  • About the access token, can I use the one generated by the doc's tool in production or do I need to implement OATH in my php app? Jan 3, 2017 at 21:50
  • 1
    The recommendation is to not use the one generated by the tool in production, the reason for this is because that is tied to you personally. You would normally like to have a unique token per application. That said, using your personally created token would work just fine, even in a production environment.
    – Robban
    Jan 3, 2017 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.