0

我正在尝试使用 PHP curl 更新我的集合中的条目,但每次都出现 404 错误。

$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是具有这种结构的关联数组:

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

整个条目还有几个字段,但我只想更新这 3 个。
添加整个条目(填充每个字段)也不起作用。
PUTPATCH方法返回相同的错误。

更新
经过一些调试,我发现/modules/Collections/Controller/RestApi.phpsave方法中,由于某种未知原因$this->module('cockpit)->getUser();返回false$this->param('data', null);返回null

最令人费解的是$this->param('data', null);返回null。当我转储整个请求对象时,我看到有我的 json 但未解析。只是平面json字符串。 请求对象片段

4

1 回答 1

0

经过一些广泛的调试后,我找到了解决方案。

我的错误是卷曲。更具体地说,我尝试设置标题的方式(我很傻)。

我不得不改变:

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

到:

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
于 2021-11-26T08:36:08.903 回答