使用Cloudant,PHP和范围查询的错误请求

时间:2018-01-25 13:23:50

标签: php curl couchdb cloudant

我正在尝试使用范围查询来处理PHP cURL和Cloudant。

我在Cloudant中设置了以下搜索索引:

{
  "_id": "_design/date",
  "_rev": "20-822ebda694f4fb57dc0a48bf60f6570f",
  "views": {},
  "language": "javascript",
  "indexes": {
    "by_date": {
      "analyzer": "classic",
      "index": "function (doc) {\n  index(\"default\", doc.datequery);\n}"
    }
  }
}

然后在我的PHP文档中,我执行以下操作:

$urlstuff = "[20180110 TO 20180119]";

$url = "https://user:pass.@user.cloudant.com/db/_design/date/_search/by_date?q=".$urlstuff."&include_docs=true&limit=200";
$ch = curl_init();   // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);

我收到了错误请求。

如果我更换:

$urlstuff = "[20180110 TO 20180119]"; 

$urlstuff = "20180117";

然后我得到了很好的结果。

方括号是否与我的cURL混乱? 我错过了什么?

1 个答案:

答案 0 :(得分:2)

是的,你走在正确的轨道上。如果您转义变量(例如使用http://php.net/manual/en/function.curl-escape.php?),您的查询应该有效:

{('key1',): 2,
 ('key2',): 1,
 ('key2', 'key3'): 0,
 ('key2', 'key4'): 1,
 ('key5',): 1,
 ('key7',): 1,
 ('key7', 'key3'): 0,
 ('key7', 'key4'): 1}
key1: 2
key2: 1
key2-key3: 0
key2-key4: 1
key5: 1
key7: 1
key7-key3: 0
key7-key4: 1

VS

curl -g -X GET   'https://user:password@user-bluemix.cloudant.com/db/_design/date/_search/by_date?q=[20180110 TO 20180119]&include_docs=true&limit=20'   


{"error":"bad_request","reason":"Bad request"}