我正在使用Elastic SQL插件进行编程工作。
查询是通过以下方式使用GET方法和cURL完成的:
curl -XGET http://localhost:9200/_sql/_explain -H 'Content-Type: application/json' \
-d 'SELECT city.keyword as city FROM routes group by city.keyword order by city'
我在公共服务器上暴露了一个自定义端口,并使用基本身份验证进行反向代理。
这段代码,加上基本身份验证头部,可以正常工作:
$host = 'http://myhost.com:9200';
$uri = "/_sql/_explain";
$auth = "john:doe";
$data = "SELECT city.keyword as city FROM routes group by city.keyword order by city";
function restCurl($host, $uri, $data = null, $auth = null, $method = 'DELETE'){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host.$uri);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($method == 'POST')
curl_setopt($ch, CURLOPT_POST, 1);
if ($auth)
curl_setopt($ch, CURLOPT_USERPWD, $auth);
if (strlen($data) > 0)
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$resp = curl_exec($ch);
if(!$resp){
$resp = (json_encode(array(array("error" => curl_error($ch), "code" => curl_errno($ch)))));
}
curl_close($ch);
return $resp;
}
$resp = restCurl($host, $uri);
$resp = restCurl($host, $uri, $data, $auth, 'GET');
$resp = restCurl($host, $uri, $data, $auth, 'POST');
$resp = restCurl($host, $uri, $data, $auth, 'PUT');
curl -d '{json-string}' -X GET http://api.mysite.com/user
,所以肯定有一种方法可以在PHP中模拟它。 - Gilberg