我是弹性搜索新手。我有一个网址。直接执行该URL时,我得到了结果。但是当我尝试使用curl运行时,我没有得到任何数据。以下是我的链接
http://localhost:9200/bank/_search?q=address:mill
以及我得到的上述链接的样本回复是
{"took":1,"timed_out":false,"_shards": {"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":4.8004513,"hits":[{"_index":"bank","_type":"account","_id":"136","_score":4.8004513,"_source":{"account_number":136,"balance":45801,"firstname":"Winnie","lastname":"Holland","age":38,"gender":"M","address":"198 Mill Lane","employer":"Neteria","email":"winnieholland@neteria.com","city":"Urie","state":"IL"}
}]}}
下面是上面网址的卷曲程序。
$url = "localhost:9200/bank/account/_search?q=mill";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);
答案 0 :(得分:0)
您需要使用 CURLOPT 的 HTTPGET 选项,然后您需要使用网址附加参数。
尝试以下代码:
<?php
try {
$url = "http://localhost:9200/bank/_search";
$search_val = 'mill';
$str = "q=address:".$search_val;
$url_final = $url.'?'.$str;
$ch = curl_init();
if (FALSE === $ch)
throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_URL, $url_final);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec ($ch);
if (FALSE === $return)
throw new Exception(curl_error($ch), curl_errno($ch));
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
curl_close ($ch);
echo $return;
?>