【发布时间】:2020-01-03 06:37:04
【问题描述】:
我在 AWS 上设置了一个 Ubuntu 18.04 实例,并在其上安装了 Elasticsearch 版本 7。我还为它分配了一个 FQDN
我用了一个例子,在上面加载了莎士比亚的全部作品。我安装了
从我的 Mac,使用终端,我 ssh'ed 到 AWS 实例(使用它的 FQDN),然后从终端,我执行以下操作:
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/shakespeare/_search?pretty' -d'
> {
> "query":
> {
> "match_phrase": {
> "text_entry":"to be or not to be"
> }
> }
> }'
我得到一个答案:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 13.889601,
"hits" : [
{
"_index" : "shakespeare",
"_type" : "_doc",
"_id" : "34229",
"_score" : 13.889601,
"_source" : {
"type" : "line",
"line_id" : 34230,
"play_name" : "Hamlet",
"speech_number" : 19,
"line_number" : "3.1.64",
"speaker" : "HAMLET",
"text_entry" : "To be, or not to be: that is the question:"
}
}
]
}
}
我尝试写一个PHP脚本,做同样的事情,如下:
<?php
$query = '{
"query":
{"match_phrase":{
"text_entry":"to be or not to be"
}
}
}';
$url = 'http://myserver.com:9200?shakespeare/_search?pretty';
$method = "GET";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$result = array('info' => curl_getinfo($ch), 'op_result' => json_decode(curl_exec($ch), TRUE));
print_r($result);
但是,我没有得到任何结果:
Array
(
[info] => Array
(
[url] => http://myserver.com:9200?shakespeare/_search?pretty
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] =>
[certinfo] => Array
(
)
[primary_port] => 0
[local_ip] =>
[local_port] => 0
)
[op_result] =>
)
有什么想法吗?
【问题讨论】:
-
您不会手动编写 HTTP 请求,是吗?我强烈建议使用a proper high-level library 而不是
curl。
标签: php elasticsearch curl