php - 如何在 PHP 中使用 cURL/HTTP 将数据插入 CloudTables 数据库?错误:需要 API 密钥
问题描述
我正在使用 cURL 请求向 CloudTables 数据库中插入一行。以下是他们的 [文档][1] 上提供的示例 cURL 请求:
curl \
-X POST \
-d key=:apiKey \
https://sub-domain.cloudtables.io/api/1/dataset/:id
在哪里
- :apiKey 是用于访问的 API 密钥(见下文)
- :id 是数据集 id(一个 UUID),
下面是我的 PHP 代码:
$post = array(
'clientId' => $user_id,
'clientName' => $user_email,
'dp-01' => $user_id,
'dp-02' => $user_type,
'dp-03' => $fullname,
'dp-04' => $address,
);
$ch = curl_init('https://sub-domain.cloudtables.io/api/1/dataset/my-dataset-id');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('key: my-api-key'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);
但每次回复都说:
[
{
"msg":"API key is required",
"name":"key"
}
]
为什么!!!发送 API 密钥的正确方法是什么?
我还尝试在 $post 数组和 URL 中发送 API 密钥,但得到相同的响应。[1]:https ://cloudtables.com/docs/cloud/api/rest/post-dataset
解决方案
为 , 传递一个数组CURLOPT_POSTFIELDS
将使它发送一个multipart/form-data
请求 - 而 API 显然期望application/x-www-form-urlencoded
。
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
改为使用
推荐阅读
- jquery - ASP.NET 中的 DateRangePicker,MasterPage 不起作用
- python-2.7 - 在 python2.7 中使用 urlopen 时出错
- python - 在 Keras 的 GPU 上微调 VGG-16:内存消耗
- c# - 单击按钮时在两个 xaml 页面之间切换
- javascript - JavaScript:Join() 和 Match() 未按预期工作
- mysql - 使用sql计算时间前
- javascript - 关闭后JavaScript计数计数器自定义小工具重置?
- google-chrome - Fantasy 字体 – 在 IE & Edge、Chrome、FF 中不同
- java - 带有文件读取的二进制搜索进入无限循环
- spring - 在终端中传递jobParameters spring批处理