cockpit "api/collections/save" 404 error PHP curl

315 views Asked by At

I’m trying to update entries in my collection with PHP curl but I got 404 error every time.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, APP_URL . '/api/collections/save/trigocms?token=' . APP_TOKEN);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, ['Content-Type' => 'application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['data' => $site]));
$output = curl_exec($ch);
curl_close($ch);

$site is an associative array with this structure:

[
    "_id" => "<identifier>"
    "active" => true
    "address" => "<some-value>"
    "key" => "<some-value>"
]

whole entry has few more fields but I want to update only those 3.
Adding whole entry (with every field filled) does not work either.
PUT and PATCH methods returns same error.

UPDATE
After some debugging I have found that in /modules/Collections/Controller/RestApi.php in save method, $this->module('cockpit)->getUser(); returns false and $this->param('data', null); returns null for some unknown reason.

Most puzzling is that $this->param('data', null); returns null. when I dumped whole request object I see that there is my json but not parsed. just plane json string. request object fragment

1

There are 1 answers

0
ciekals11 On BEST ANSWER

After some extensive debugging I have found a solution.

My error was in curl. More specifically in the way that I tried to set headers (silly me).

I had to change:

curl_setopt($ch, CURLOPT_HEADER, ['Content-Type' => 'application/json']);

to:

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);