0

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

ciekals11
  • 2,032
  • 1
  • 10
  • 24
  • Check CURLOPT_URL, APP_URL are in scope and test echo the full URL and see if your concatenation is valid. If you can copy paste in a browser and see if a 404 is returned as wel. – Oliver M Grech Nov 25 '21 at 10:09
  • @OliverMGrech `APP_URL` and `APP_TOKEN` constants are declared in global scope and are accessible in this script. I suspected same cause but I turned out that both are valid and concatenated url is valid as well. After pasting it into browser there is 404 error. – ciekals11 Nov 25 '21 at 10:12
  • seems to me you have a wrong URL or who provided your endpoint gave you the wrong URL. You cant fix this in your CURL script. Check the server side stuff (or whoever is responsible) – Oliver M Grech Nov 25 '21 at 14:04
  • well. I'm. cockpit is running on pc in my local network. Let's say its hostname is `random-pc`. Then my url is `http://random-pc/api/collections/save/trigocms?token=` and I'm sure that this is correct url because `http://random-pc/api/collection/get/trigocms?token=` returns list of entries. – ciekals11 Nov 25 '21 at 14:09
  • are you using an IP address or a pc name / hostname? your PHP script is running on a linux server or on a local WAMP server or similar? – Oliver M Grech Nov 25 '21 at 14:34
  • @OliverMGrech hostname, wamp on windows 10 – ciekals11 Nov 25 '21 at 14:35

1 Answers1

0

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']);
ciekals11
  • 2,032
  • 1
  • 10
  • 24