Unable to update collection using API using PHP

Hello,
I am trying to update a collection item using API. I am using PHP to do the same. However, it’s not working, what am i doing wrong? There seems to be some issue with the “fields” data. I am following the steps mentioned here - Webflow CMS API Reference

<?php
$service_url = 'https://api.webflow.com/collections/##/items/##?live=true';
$curl = curl_init($service_url);

$headers = [
    'Accept-Version: 1.0.0',
    'Authorization: Bearer ###',
    'content-type: application/json',
    'accept: application/json'
];

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);


$data = array("fields" => array("view_count" => 1));
$data_string = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, array("data"=>$data_string));
echo $data_string;


$curl_response = curl_exec($curl);
curl_close($curl);
$json_objekat=json_decode($curl_response);

var_dump($json_objekat);

?>

Here is my public share link: LINK
(how to access public share link)

Yikes! I haven’t used PHP for awhile.

Can you reproduce in Postman or Insomnia?

This will help you narrow down if it’s your code, your api call, or just the api itself and you’ll know exactly where to start fixing it.

I last did coding a few years ago and unfortunately PHP is the only backend language i know :slight_smile:

I tried postman but couldn’t figure out how to send fields json data as part of the request

No worries.

Since you can code (I usually don’t suggest this otherwise), maybe a good first step is to then just try the curl statement: Webflow CMS API Reference

…and swap in your API token and id’s.

They give you a nice example of fields.

If you’re wondering what those field names translate to in the Webflow CMS, create a new CMS Collection and use the pre-built “Blog Posts” collection template.

That will demonstrate how the Webflow CMS field is spelled, with casing, and how that spelling and casing changes when you need to use the API.

Example: “Post Body” → “post-body”

Thanks @ChrisDrit.

I am getting the same error in postman as well. As per the documentation, it’s happening because - “Request body was incorrectly formatted. Likely invalid JSON being sent up.” It seems to be happening because of how I am passing the data field. Also, is there any other way you can suggest? I am looking to update certain columns in a collection when user clicks on a button on the page

{
“msg”: “Invalid request body”,
“code”: 400,
“name”: “ValidationError”,
“path”: “/collections/##/items/##”,
“err”: “ValidationError: Invalid request body”
}

Yea, so… setup a free Make.com account (if you don’t already have one) and do all of this from a Webflow module.

That should work.

When it does… you can inspect the request sent to the Webflow API (from your module).

You’ll see the headers, the JSON blob, everything.

Now you can copy/paste that into your curl statement and confirm.

If it works in Make but not in curl, you’re doing something wrong. Post your curl statement here.

If it works in curl, then move towards getting it to work via your PHP script.

:+1:

Was able to get postman to work. Also, found out that postman also has the option of seeing code snippet. Used it and was able to make the end to end flow work.

Thanks @ChrisDrit