简体   繁体   中英

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

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. 请求对象片段

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']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM