无法更新数据集:无法将资源作为字典列表发送

Unable to update dataset: can't send resources as list of dictionaries

我们已经使用 CKAN API(运行ning ckan 版本 2.5.7)在 PHP 中构建了一组脚本,但是我们还没有成功制作package_update 电话。如果我们不发送带有 POST 数据的资源,那么它们都会被删除,这是我们不希望的。但是我还没有找到 CKAN 会接受的发送方式。

我们正在使用 cURL:

curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$dictionary);

其中

$dictionary['resources'] = json_encode(
    array_map(
        function($resource) {
            return filter_out_unwanted_fields($resource); // returns an associative array
        },
        $all_resources_for_the_dataset
    )
);

当我们 运行 调用 API 时,我们得到错误:Only lists of dicts can be placed against subschema ('resources',), not <type 'unicode'>

我将此解释为对 POSTed 参数是 json_encode() 返回的 unicode 字符串的抱怨。那么,在不删除所有资源的情况下,我可以通过 HTTP POST 发送什么来让 API 满意?

通过将数组传递到 curl_setopt($ch,CURLOPT_POSTFIELDS,$dictionary);,您将以 multipart/form-data (reference) 的形式发送数据。

所以 CKAN 正在解包数据,并以 resource 的字符串结尾——该字符串的内容是您的序列化数据:("[{}, {}, {}]")

我非常有信心 CKAN 会接受整个表单数据作为一个 JSON 对象,所以如果你移动 json_encode 来编码整个 $dictionary(并且可能添加一个Content-Type header?) 然后 CKAN 应该完全解开它,从而为您提供 resource ([{}, {}, {}])

的 Dict 对象列表