【发布时间】:2012-07-20 21:29:21
【问题描述】:
我一直在尝试使用 MongoLabs api 来简化我的生活,并且在我尝试使用 php 和 curl 将更新推送到数据库之前,它大部分都在工作,无论如何都没有骰子。我的代码是这样的:
$data_string = json_encode('{"user.userEmail": "USER_EMAIL", "user.pass":"USER_PASS"}');
try {
$ch = curl_init();
//need to create temp file to pass to curl to use PUT
$tempFile = fopen('php://temp/maxmemory:256000', 'w');
if (!$tempFile) {
die('could not open temp memory data');
}
fwrite($tempFile, $data_string);
fseek($tempFile, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
//curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
//curl_setopt($ch, CURLOPT_INFILE, $tempFile); // file pointer
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, DB_API_REQUEST_TIMEOUT);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
)
);
$cache = curl_exec($ch);
curl_close($ch);
} catch (Exception $e) {
return FALSE;
}
我的问题似乎出在 MongoLab 的 api 上。除了实验室告诉我我传递的数据是'无效对象{“user.firstName”:“Pablo”,“user.newsletter”:“true”}:存储在db 不能有 .在他们中。'。我尝试过传递一个文件并使用 postfields,但都没有成功。
当我在 Firefox 的海报插件上对其进行测试时,该值工作正常。如果有人对 MongoLabs 的东西有更好的理解,我会喜欢一些启发。提前致谢!
【问题讨论】:
-
我会从字面上理解错误信息。字段名称不能带有“。”在其中,所以尝试 user_firstName、user_pass 或类似的。
-
我明白这一点,但在后面的部分中,我能够使用相同的样本测试 api,并且它运行良好。因此,据我了解,问题不在于数据太多,而在于我如何将其传递给 api。将其分配给允许按原样阅读的正文/内容的方式有所不同。